<?php
namespace app\index\controller;

use app\index\model\AccountModel;
use app\index\model\BidModel;
use app\index\model\CertModel;
use app\index\model\CertUseListModel;
use app\index\model\ContractCaiModel;
use app\index\model\ContractChuModel;
use app\index\model\ContractNeiModel;
use app\index\model\ContractShiJieModel;
use app\index\model\ContractShiModel;
use app\index\model\CossJiaoModel;
use app\index\model\CossJieListModel;
use app\index\model\CossJieModel;
use app\index\model\CossModel;
use app\index\model\DealcomModel;
use app\index\model\DeptModel;
use app\index\model\DossJiaoModel;
use app\index\model\DossJieListModel;
use app\index\model\DossJieModel;
use app\index\model\DossModel;
use app\index\model\EnginModel;
use app\index\model\EnginShuiModel;
use app\index\model\FileModel;
use app\index\model\FiletypeModel;
use app\index\model\FlowBillModel;
use app\index\model\FlowLogModel;
use app\index\model\HouBaoListModel;
use app\index\model\HouBaoModel;
use app\index\model\HouBaoPiaoModel;
use app\index\model\HouChujinGuanModel;
use app\index\model\HouChujinListModel;
use app\index\model\HouChujinModel;
use app\index\model\HouChushuiListModel;
use app\index\model\HouChushuiModel;
use app\index\model\HouDayshouModel;
use app\index\model\HouDayzhiGuanModel;
use app\index\model\HouDayzhiListModel;
use app\index\model\HouDayzhiModel;
use app\index\model\HouGonghuiListModel;
use app\index\model\HouGonghuiModel;
use app\index\model\HouGongshuiFuModel;
use app\index\model\HouGongshuiGuanModel;
use app\index\model\HouGongshuiListModel;
use app\index\model\HouGongshuiModel;
use app\index\model\HouGongyuModel;
use app\index\model\HouGongzhiGuanModel;
use app\index\model\HouGongzhiListModel;
use app\index\model\HouGongzhiModel;
use app\index\model\HouGongzhiQuModel;
use app\index\model\HouJieListModel;
use app\index\model\HouJieModel;
use app\index\model\HouKouFuwuModel;
use app\index\model\HouKouGongjieModel;
use app\index\model\HouKouKuaifeiModel;
use app\index\model\HouKouQifeiModel;
use app\index\model\HouKouShuifeiModel;
use app\index\model\HouShouModel;
use app\index\model\HouTiaoModel;
use app\index\model\KaoaccModel;
use app\index\model\KuaiJiModel;
use app\index\model\KuaiShouModel;
use app\index\model\PiaoChuListModel;
use app\index\model\PiaoDayListModel;
use app\index\model\PiaoKaiListModel;
use app\index\model\PiaokaiModel;
use app\index\model\PiaoShouListModel;
use app\index\model\SealZhiModel;
use app\index\model\SuoHuiModel;
use app\index\model\UserModel;
use app\index\model\XossJiaoModel;
use app\index\model\XossJieListModel;
use app\index\model\XossJieModel;
use app\index\model\XossModel;
use app\index\relevan\CertRelevan;
use app\index\relevan\EnginRelevan;
use app\index\service\CertService;
use app\index\service\ContractService;
use app\index\service\EnginService;
use app\index\service\FinanceService;
use app\index\service\FlowService;
use app\index\service\HouService;
use app\index\service\MenuService;
use app\index\service\OptService;
use app\index\service\UserService;
use app\index\service\XingService;
use think\Db;
use think\Exception;
use think\facade\Config;
use think\facade\Env;
use think\Request;

class Hou extends Common
{

    public function detail_hou_chushui(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['shui.deleted_user','=', 0];
        $where[] = ['shui.comid','=',$this->userinfo['comid']];
        $where[] = ['shui.isturn','=',1];
        $where[] = ['shui.status','in',[0,1]];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['bill.sericnum|shui.lianer|list.fuer','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['sericnum']) && !empty($param['sericnum'])){
            $where[] = ['bill.sericnum','like', '%'.$param['sericnum'].'%'];
        }
        if(isset($param['lianer']) && !empty($param['lianer'])){
            $where[] = ['shui.lianer','like', '%'.$param['lianer'].'%'];
        }
        if(isset($param['fuer']) && !empty($param['fuer'])){
            $where[] = ['list.fuer','like', '%'.$param['fuer'].'%'];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['shui.status','=', $param['status']];
        }
        if(isset($param['fulei']) && $param['fulei'] != ''){
            $where[] = ['list.fulei','=', $param['fulei']];
        }

        $data['data'] = HouChushuiListModel::alias('list')
            ->field('shui.*,bill.sericnum,list.fulei,
            list.id as listid,list.fuer,list.yiju,list.jishuifee,list.yijurate,list.money as listmoney,list.explain as listexplain,
            list.incdaotype,list.banknum,list.bankname')
            ->join('hou_chushui shui','shui.id=list.chushuiid')
            ->leftJoin('flow_bill bill','bill.id=shui.billid')
            ->where($where)
            ->order('shui.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['yiju_name'] = (new HouService())->get_hou_chushui_yiju_name($item['yiju']);
                $item['incdaotype_name'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);

                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_hou_chushui(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['shui.deleted_user','=', 0];
        $where[] = ['shui.comid','=',$this->userinfo['comid']];
        $where[] = ['shui.isturn','=',1];
        $where[] = ['shui.status','in',[0,1]];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['shui.lianer|shui.zhai','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['lianer']) && !empty($param['lianer'])){
            $where[] = ['shui.lianer','like', '%'.$param['lianer'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['shui.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['shui.status','=', $param['status']];
        }


        $data['data'] = HouChushuiModel::alias('shui')
            ->field('shui.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('hou_chushui', 'shui'))
            ->order('shui.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['zhiqilei_name'] = (new OptService())->get_base_showname('cai_zhiqilei', $item['zhiqilei']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('hou_chushui');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_hou_chushui(Request $request){
        $param = $request->post();
        $data = HouChushuiModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            HouChushuiModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'hou_chushui', $data['id'], '储备税费支付');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_hou_chushui(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = HouChushuiModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['data']['zhiqilei_name'] = (new OptService())->get_base_showname('cai_zhiqilei', $data['data']['zhiqilei']);

        $list = HouChushuiListModel::where(['chushuiid'=>$data['data']['id']])->select()->toArray();
        foreach ($list as &$item){
            $item['incdaotype_name'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);
            $item['yiju_name'] = (new HouService())->get_hou_chushui_yiju_name($item['yiju']);

            $item['lei_kuan'] = HouChujinListModel::alias('list')
                ->join('hou_chujin hou','hou.id=list.chujinid')
                ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                ->where(['list.fuer'=>$item['fuer']])
                ->sum('list.money');
            $item['lei_piao'] = PiaoChuListModel::alias('list')
                ->join('piao_chu hou','hou.id=list.piaochuid')
                ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                ->where(['list.xiaoer'=>$item['fuer']])
                ->sum('list.money');
            $item['lei_shui'] = HouChushuiListModel::alias('list')
                ->join('hou_chushui hou','hou.id=list.chushuiid')
                ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                ->where(['list.fuer'=>$item['fuer']])
                ->sum('list.money');

            $item['kuaninfo'] = (new FinanceService())->get_acckuan_info_bybillid($data['data']['billid'], $item['id']);

        }
        $data['list'] = $list;


        $data['files'] = (new OptService())->get_file('hou_chushui', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }


    public function detail_hou_chujin(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['jin.deleted_user','=', 0];
        $where[] = ['jin.comid','=',$this->userinfo['comid']];
        $where[] = ['jin.isturn','=',1];
        $where[] = ['jin.status','in',[0,1]];

        if(isset($param['listid']) && !empty($param['listid'])){
            $where[] = ['list.id','=', $param['listid']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['bill.sericnum|jin.lianer|list.fuer','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['sericnum']) && !empty($param['sericnum'])){
            $where[] = ['bill.sericnum','like', '%'.$param['sericnum'].'%'];
        }
        if(isset($param['lianer']) && !empty($param['lianer'])){
            $where[] = ['jin.lianer','like', '%'.$param['lianer'].'%'];
        }
        if(isset($param['fuer']) && !empty($param['fuer'])){
            $where[] = ['list.fuer','like', '%'.$param['fuer'].'%'];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['jin.status','=', $param['status']];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $created_atArr = explode(' - ', $param['created_at']);
            $where[] = ['jin.created_at','between', [$created_atArr[0].' 00:00:00', $created_atArr[1].' 23:59:59']];
        }

        $list = [];
        $info = HouChujinListModel::alias('list')
            ->field('jin.*,bill.sericnum,con.name as conname,
            list.id as listid,list.fuer,list.money as listmoney,list.explain as listexplain,list.incdaotype,
            list.banknum,list.bankname,list.difee,list.chuid')
            ->join('hou_chujin jin','jin.id=list.chujinid')
            ->leftJoin('flow_bill bill','bill.id=jin.billid')
            ->leftJoin('contract_chu con','con.id=list.chuid')
            ->where($where)
            ->order('jin.id DESC')
            ->select()->toArray();
        foreach ($info as &$item){
            $item['incdaotype_name'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);


            $item['yishoupiao'] = HouChujinGuanModel::alias('guan')
                ->field('list.*')
                ->join('hou_chujin jin','jin.id=guan.chujinid')
                ->join('piao_chu piao','piao.id=guan.piaoid')
                ->where(['jin.comid'=>$this->userinfo['comid'],'jin.deleted_user'=>0,'jin.isturn'=>1,'jin.status'=>1])
                ->where(['piao.comid'=>$this->userinfo['comid'],'piao.deleted_user'=>0,'piao.isturn'=>1,'piao.status'=>1])
                ->where(['guan.chujinlistid'=>$item['listid']])
                ->sum('difee');

            if($item['yishoupiao'] <= 0){
                $item['statename'] = '未收票';
            }else{
                if($item['yishoupiao'] < $item['listmoney']){
                    $item['statename'] = '部分收票';
                }else{
                    $item['statename'] = '已收票';
                }
            }
            $item['daishoufee'] = round($item['listmoney'] - $item['yishoupiao'], 2);


            if(isset($param['statename']) && !empty($param['statename'])){
                if($param['statename'] == $item['statename']){
                    $list[] = $item;
                }
            }else{
                $list[] = $item;
            }
        }

        $data['data'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function detail_hou_chujin222(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['jin.deleted_user','=', 0];
        $where[] = ['jin.comid','=',$this->userinfo['comid']];
        $where[] = ['jin.isturn','=',1];
        $where[] = ['jin.status','in',[0,1]];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['bill.sericnum|jin.lianer|list.fuer','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['sericnum']) && !empty($param['sericnum'])){
            $where[] = ['bill.sericnum','like', '%'.$param['sericnum'].'%'];
        }
        if(isset($param['lianer']) && !empty($param['lianer'])){
            $where[] = ['jin.lianer','like', '%'.$param['lianer'].'%'];
        }
        if(isset($param['fuer']) && !empty($param['fuer'])){
            $where[] = ['list.fuer','like', '%'.$param['fuer'].'%'];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['jin.status','=', $param['status']];
        }

        $data['data'] = HouChujinListModel::alias('list')
            ->field('jin.*,bill.sericnum,con.name as conname,
            list.id as listid,list.fuer,list.money as listmoney,list.explain as listexplain,list.incdaotype,
            list.banknum,list.bankname,list.difee,list.chuid')
            ->join('hou_chujin jin','jin.id=list.chujinid')
            ->leftJoin('flow_bill bill','bill.id=jin.billid')
            ->leftJoin('contract_chu con','con.id=list.chuid')
            ->where($where)
            ->order('jin.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['incdaotype_name'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);


                $item['yishoupiao'] = HouChujinGuanModel::alias('guan')
                    ->field('list.*')
                    ->join('hou_chujin jin','jin.id=guan.chujinid')
                    ->join('piao_chu piao','piao.id=guan.piaoid')
                    ->where(['jin.comid'=>$this->userinfo['comid'],'jin.deleted_user'=>0,'jin.isturn'=>1,'jin.status'=>1])
                    ->where(['piao.comid'=>$this->userinfo['comid'],'piao.deleted_user'=>0,'piao.isturn'=>1,'piao.status'=>1])
                    ->where(['guan.chujinlistid'=>$item['listid']])
                    ->sum('difee');

                if($item['yishoupiao'] <= 0){
                    $item['statename'] = '未收票';
                }else{
                    if($item['yishoupiao'] < $item['listmoney']){
                        $item['statename'] = '部分收票';
                    }else{
                        $item['statename'] = '已收票';
                    }
                }
                $item['daishoufee'] = round($item['listmoney'] - $item['yishoupiao'], 2);

                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_hou_chujin(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['jin.deleted_user','=', 0];
        $where[] = ['jin.comid','=',$this->userinfo['comid']];
        $where[] = ['jin.isturn','=',1];
        $where[] = ['jin.status','in',[0,1]];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['jin.lianer|jin.zhai','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['lianer']) && !empty($param['lianer'])){
            $where[] = ['jin.lianer','like', '%'.$param['lianer'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['jin.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['jin.status','=', $param['status']];
        }


        $data['data'] = HouChujinModel::alias('jin')
            ->field('jin.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('hou_chujin', 'jin'))
            ->order('jin.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['zhiqilei_name'] = (new OptService())->get_base_showname('cai_zhiqilei', $item['zhiqilei']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('hou_chujin');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_hou_chujin(Request $request){
        $param = $request->post();
        $data = HouChujinModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            HouChujinModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'hou_chujin', $data['id'], '储备进项支付');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_hou_chujin(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = HouChujinModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['data']['zhiqilei_name'] = (new OptService())->get_base_showname('cai_zhiqilei', $data['data']['zhiqilei']);

        $list = HouChujinListModel::where(['chujinid'=>$data['data']['id']])->select()->toArray();
        foreach ($list as &$item){
            $item['incdaotype_name'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);

            $item['chuid_name'] = '';
            $chuInfo = ContractChuModel::where(['id'=>$item['chuid']])->find();
            if($chuInfo){
                $item['chuid_name'] = $chuInfo['name'];
                $item['lei_kuan'] = HouChujinListModel::alias('list')
                    ->join('hou_chujin hou','hou.id=list.chujinid')
                    ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                    ->where(['list.chuid'=>$item['chuid']])
                    ->sum('list.money');
                $item['lei_piao'] = PiaoChuListModel::alias('list')
                    ->join('piao_chu hou','hou.id=list.piaochuid')
                    ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                    ->where(['list.chuid'=>$item['chuid']])
                    ->sum('list.money');
                $item['lei_chumoney'] = ContractChuModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['isturn'=>1,'status'=>1,'id'=>$item['chuid']])
                    ->sum('money');
            }else{
                $item['lei_kuan'] = HouChujinListModel::alias('list')
                    ->join('hou_chujin hou','hou.id=list.chujinid')
                    ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                    ->where(['list.fuer'=>$item['fuer']])
                    ->sum('list.money');
                $item['lei_piao'] = PiaoChuListModel::alias('list')
                    ->join('piao_chu hou','hou.id=list.piaochuid')
                    ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                    ->where(['list.xiaoer'=>$item['fuer']])
                    ->sum('list.money');
                $item['lei_chumoney'] = ContractChuModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['isturn'=>1,'status'=>1,'gonger'=>$item['fuer']])
                    ->sum('money');
            }

            $item['kuaninfo'] = (new FinanceService())->get_acckuan_info_bybillid($data['data']['billid'], $item['id']);

        }
        $data['list'] = $list;


        $data['files'] = (new OptService())->get_file('hou_chushui', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }
    public function list_piao_chu_guan(Request $request){
        $xiaoer = $request->post('xiaoer', '', 'string');
        if(!$xiaoer){
            return outputJson(2000,lang('code_2000'),['list'=>[]]);
        }

        $list = [];
        $chuList = PiaoChuListModel::alias('list')
            ->field('list.*')
            ->join('piao_chu hou','hou.id=list.piaochuid')
            ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
            ->where(['list.xiaoer'=>$xiaoer])
            ->select()->toArray();
        foreach ($chuList as $item){
            $item['piaoid'] = $item['piaochuid'];
            $item['piaolistid'] = $item['id'];
            $item['chuid_name'] = ContractChuModel::where(['id'=>$item['chuid']])->value('name');
            $item['qianpiaotype_name'] = (new OptService())->get_base_showname('cai_qianpiaotype', $item['qianpiaotype']);

            $item['yifufee'] = HouChujinGuanModel::alias('guan')
                ->field('list.*')
                ->join('hou_chujin jin','jin.id=guan.chujinid')
                ->join('piao_chu piao','piao.id=guan.piaoid')
                ->where(['jin.comid'=>$this->userinfo['comid'],'jin.deleted_user'=>0,'jin.isturn'=>1,'jin.status'=>1])
                ->where(['piao.comid'=>$this->userinfo['comid'],'piao.deleted_user'=>0,'piao.isturn'=>1,'piao.status'=>1])
                ->where(['guan.piaolistid'=>$item['id']])
                ->sum('difee');
            if($item['yifufee'] <= 0){
                $item['statename'] = '未付款';
            }else{
                if($item['yifufee'] < $item['money']){
                    $item['statename'] = '部分付款';
                }else{
                    continue;
                }
            }
            $item['daifufee'] = round($item['money'] - $item['yifufee'], 2);

            $list[] = $item;
        }

        $data['list'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }



    public function detailfu_hou_gongshui(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['zhi.deleted_user','=', 0];
        $where[] = ['zhi.comid','=',$this->userinfo['comid']];
        $where[] = ['zhi.isturn','=',1];
        $where[] = ['zhi.status','in',[0,1]];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['zhi.enginid','=', $param['enginid']];
        }
        if(isset($param['listid']) && !empty($param['listid'])){
            $where[] = ['list.id','=', $param['listid']];
        }


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['bill.sericnum|e.name|e.number|e.gofuer|list.fuer|zhi.flowuser','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['sericnum']) && !empty($param['sericnum'])){
            $where[] = ['bill.sericnum','like', '%'.$param['sericnum'].'%'];
        }
        if(isset($param['engin_name']) && !empty($param['engin_name'])){
            $where[] = ['e.name','like', '%'.$param['engin_name'].'%'];
        }
        if(isset($param['engin_number']) && !empty($param['engin_number'])){
            $where[] = ['e.number','like', '%'.$param['engin_number'].'%'];
        }
        if(isset($param['engin_state']) && !empty($param['engin_state'])){
            $where[] = ['e.state','=', $param['engin_state']];
        }
        if(isset($param['engin_gofuer']) && !empty($param['engin_gofuer'])){
            $where[] = ['e.gofuer','like', '%'.$param['engin_gofuer'].'%'];
        }
        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['zhi.flowuser','like', '%'.$param['flowuser'].'%'];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $created_atArr = explode(' - ', $param['created_at']);
            $where[] = ['zhi.created_at','between', [$created_atArr[0].' 00:00:00', $created_atArr[1].' 23:59:59']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['zhi.status','=', $param['status']];
        }

        if(isset($param['incdaotype']) && !empty($param['incdaotype'])){
            $where[] = ['list.incdaotype','=', $param['incdaotype']];
        }
        if(isset($param['fuer']) && !empty($param['fuer'])){
            $where[] = ['list.fuer','like', '%'.$param['fuer'].'%'];
        }


        $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
        $data['is_incdaotype'] = isset($param['incdaotype']) ? $param['incdaotype'] : '';

        $data['engin_state'] = (new EnginService())->get_engin_state();
        $data['is_engin_state'] = isset($param['engin_state']) ? $param['engin_state'] : '';

        $data['data'] = HouGongshuiFuModel::alias('list')
            ->field('zhi.*,e.name as enginname,e.number as enginnumber,e.gofuer,e.state,bill.sericnum,
            list.id as listid,list.fuer,list.money as listmoney,list.explain as listexplain,list.incdaotype,
            list.bankzhang,list.banknum,list.bankname')
            ->join('hou_gongshui zhi','zhi.id=list.gongshuiid')
            ->join('flow_bill bill','bill.id=zhi.billid')
            ->join('engin e','e.id=zhi.enginid')
            ->where($where)
            ->group('list.id DESC')
            ->order('zhi.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['incdaotype_name'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);

                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function detail_hou_gongshui(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['shui.deleted_user','=', 0];
        $where[] = ['shui.comid','=',$this->userinfo['comid']];
        $where[] = ['shui.isturn','=',1];
        $where[] = ['shui.status','in',[0,1]];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['shui.enginid','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['bill.sericnum|e.name|e.number|e.gofuer|list.xiaoer|shui.fuer|shui.flowuser','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['sericnum']) && !empty($param['sericnum'])){
            $where[] = ['bill.sericnum','like', '%'.$param['sericnum'].'%'];
        }
        if(isset($param['engin_name']) && !empty($param['engin_name'])){
            $where[] = ['e.name','like', '%'.$param['engin_name'].'%'];
        }
        if(isset($param['engin_number']) && !empty($param['engin_number'])){
            $where[] = ['e.number','like', '%'.$param['engin_number'].'%'];
        }
        if(isset($param['engin_state']) && !empty($param['engin_state'])){
            $where[] = ['e.state','=', $param['engin_state']];
        }
        if(isset($param['engin_gofuer']) && !empty($param['engin_gofuer'])){
            $where[] = ['e.gofuer','like', '%'.$param['engin_gofuer'].'%'];
        }
        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['shui.flowuser','like', '%'.$param['flowuser'].'%'];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $created_atArr = explode(' - ', $param['created_at']);
            $where[] = ['shui.created_at','between', [$created_atArr[0].' 00:00:00', $created_atArr[1].' 23:59:59']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['shui.status','=', $param['status']];
        }
        if(isset($param['fuer']) && !empty($param['fuer'])){
            $where[] = ['shui.fuer','like', '%'.$param['fuer'].'%'];
        }
        if(isset($param['xiaoer']) && !empty($param['xiaoer'])){
            $where[] = ['list.xiaoer','like', '%'.$param['xiaoer'].'%'];
        }


        $data['engin_state'] = (new EnginService())->get_engin_state();
        $data['is_engin_state'] = isset($param['engin_state']) ? $param['engin_state'] : '';

        $data['data'] = HouGongshuiListModel::alias('list')
            ->field('shui.*,e.name as enginname,e.number as enginnumber,e.gofuer,e.state,bill.sericnum,gui_unit.name as gui_unit_name,
            list.id as listid,list.xiaoer,list.money as listmoney,list.explain as listexplain,list.ratefee as listratefee')
            ->join('hou_gongshui shui','shui.id=list.gongshuiid')
            ->leftJoin('flow_bill bill','bill.id=shui.billid')
            ->join('engin e','e.id=shui.enginid')
            ->leftJoin('dept gui_unit','gui_unit.id=e.gui_unit')
            ->where($where)
            ->order('shui.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['tuifang_name'] = (new HouService())->get_hou_gongshui_tuifang_name($item['tuifang']);
                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_hou_gongshui(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['shui.deleted_user','=', 0];
        $where[] = ['shui.comid','=',$this->userinfo['comid']];
        $where[] = ['shui.isturn','=',1];
        $where[] = ['shui.status','in',[0,1]];


        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['shui.enginid','=', $param['enginid']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|e.number|e.gofuer|shui.flowuser|shui.fuer','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['engin_name']) && !empty($param['engin_name'])){
            $where[] = ['e.name','like', '%'.$param['engin_name'].'%'];
        }
        if(isset($param['engin_number']) && !empty($param['engin_number'])){
            $where[] = ['e.number','like', '%'.$param['engin_number'].'%'];
        }
        if(isset($param['engin_state']) && !empty($param['engin_state'])){
            $where[] = ['e.state','=', $param['engin_state']];
        }
        if(isset($param['engin_gofuer']) && !empty($param['engin_gofuer'])){
            $where[] = ['e.gofuer','like', '%'.$param['engin_gofuer'].'%'];
        }
        if(isset($param['gui_unit']) && !empty($param['gui_unit'])){
            $where[] = ['gui_unit.id','=', $param['gui_unit']];
        }
        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['shui.flowuser','like', '%'.$param['flowuser'].'%'];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $created_atArr = explode(' - ', $param['created_at']);
            $where[] = ['shui.created_at','between', [$created_atArr[0].' 00:00:00', $created_atArr[1].' 23:59:59']];
        }
        if(isset($param['rendt']) && !empty($param['rendt'])){
            $where[] = ['shui.rendt','between', explode(' - ', $param['rendt'])];
        }
        if(isset($param['tuifang']) && !empty($param['tuifang'])){
            $where[] = ['shui.tuifang','=', $param['tuifang']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['shui.status','=', $param['status']];
        }


        $data['engin_state'] = (new EnginService())->get_engin_state();
        $data['is_engin_state'] = isset($param['engin_state']) ? $param['engin_state'] : '';

        $data['gui_unit'] = (new UserService())->get_chaninfo_all();
        $data['is_gui_unit'] = isset($param['gui_unit']) ? $param['gui_unit'] : '';

        $data['tuifang'] = (new HouService())->get_hou_gongshui_tuifang();
        $data['is_tuifang'] = isset($param['tuifang']) ? $param['tuifang'] : '';

        $data['data'] = HouGongshuiModel::alias('shui')
            ->field('shui.*,e.name as enginname,e.gofuer as engin_gofuer,gui_unit.name as gui_unit_name')
            ->join('engin e','e.id=shui.enginid')
            ->leftJoin('dept gui_unit','gui_unit.id=e.gui_unit')
            ->where($where)
            ->where((new MenuService())->get_miss_where('hou_gongshui', 'shui'))
            ->order('shui.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['zhiqilei_name'] = (new OptService())->get_base_showname('cai_zhiqilei', $item['zhiqilei']);
                $item['tuifang_name'] = (new HouService())->get_hou_gongshui_tuifang_name($item['tuifang']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('hou_gongshui');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_hou_gongshui(Request $request){
        $param = $request->post();
        $data = HouGongshuiModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            HouGongshuiModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'hou_gongshui', $data['id'], '进项税额抵扣退税');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_hou_gongshui(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = HouGongshuiModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);

        $data['data']['zhiqilei_name'] = (new OptService())->get_base_showname('cai_zhiqilei', $data['data']['zhiqilei']);
        $data['data']['tuifang_name'] = (new HouService())->get_hou_gongshui_tuifang_name($data['data']['tuifang']);



        $list = HouGongshuiListModel::where(['gongshuiid'=>$data['data']['id']])->select()->toArray();
        foreach ($list as &$item){
            $item['gongzhifee'] = HouGongzhiListModel::alias('list')
                ->join('hou_gongzhi hou','hou.id=list.gongzhiid')
                ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                ->where(['hou.enginid'=>$data['data']['enginid'],'list.fuer'=>$item['xiaoer']])
                ->sum('list.money');
            $item['piaoshoufee'] = PiaoShouListModel::alias('list')
                ->join('piao_shou shou','shou.id=list.shouid')
                ->where(['shou.comid'=>$this->userinfo['comid'],'shou.deleted_user'=>0,'shou.isturn'=>1,'shou.status'=>1])
                ->where(['shou.enginid'=>$data['data']['enginid'],'list.xiaoer'=>$item['xiaoer']])
                ->sum('list.money');

            $item['concaimoney'] = (new HouService())->get_contract_cai_final_money($data['data']['enginid'],$item['xiaoer']);

            $item['kedifee'] = 0;
            if($item['gongzhifee'] > 0 && $item['piaoshoufee'] > 0){
                $item['kedifee'] = $item['gongzhifee'] > $item['piaoshoufee'] ? $item['piaoshoufee'] : $item['gongzhifee'];
            }

            $item['yidimoney'] = HouGongshuiListModel::alias('list')
                ->join('hou_gongshui hou','hou.id=list.gongshuiid')
                ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                ->where(['hou.enginid'=>$data['data']['enginid'],'list.xiaoer'=>$item['xiaoer']])
                ->sum('list.money');
            $item['yidiratefee'] = HouGongshuiListModel::alias('list')
                ->join('hou_gongshui hou','hou.id=list.gongshuiid')
                ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                ->where(['hou.enginid'=>$data['data']['enginid'],'list.xiaoer'=>$item['xiaoer']])
                ->sum('list.ratefee');

            $item['shengdifee'] = round($item['kedifee'] - $item['yidimoney'], 2);
            $item['shengdifee'] = $item['shengdifee'] > 0 ? $item['shengdifee'] : 0;
        }
        $data['list'] = $list;


        $fu = HouGongshuiFuModel::where(['gongshuiid'=>$data['data']['id']])->select()->toArray();
        foreach ($fu as &$mmmm){
            $mmmm['incdaotype_name'] = (new OptService())->get_base_showname('cai_income_incdaotype', $mmmm['incdaotype']);
            $mmmm['kuaninfo'] = (new FinanceService())->get_acckuan_info_bybillid($data['data']['billid'], $mmmm['id']);
        }
        $data['fu'] = $fu;


        $data['files'] = (new OptService())->get_file('hou_gongshui', $data['data']['id']);
        $data['kuaninfo'] = (new FinanceService())->get_acckuan_info_bybillid($data['data']['billid']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }
    public function list_piao_shou_guan(Request $request){
        $enginid = $request->post('enginid', 0, 'int');
        $xiaoer = $request->post('xiaoer', '', 'string');
        if(!$xiaoer){
            return outputJson(2000,lang('code_2000'),['list'=>[]]);
        }

        $list = [];
        $chuList = PiaoShouListModel::alias('list')
            ->field('list.*')
            ->join('piao_shou hou','hou.id=list.shouid')
            ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
            ->where(['hou.enginid'=>$enginid,'list.xiaoer'=>$xiaoer])
            ->select()->toArray();

        foreach ($chuList as $item){
            $item['piaoid'] = $item['shouid'];
            $item['piaolistid'] = $item['id'];
            $item['qianpiaotype_name'] = (new OptService())->get_base_showname('cai_qianpiaotype', $item['qianpiaotype']);

            $yiguan = HouGongshuiGuanModel::alias('guan')
                ->join('hou_gongshui shui','shui.id=guan.gongshuiid')
                ->join('piao_shou piao','piao.id=guan.piaoid')
                ->where(['shui.comid'=>$this->userinfo['comid'],'shui.deleted_user'=>0,'shui.isturn'=>1,'shui.status'=>1])
                ->where(['piao.comid'=>$this->userinfo['comid'],'piao.deleted_user'=>0,'piao.isturn'=>1,'piao.status'=>1])
                ->where(['guan.piaolistid'=>$item['id']])
                ->find();

            if(!$yiguan){
                $list[] = $item;
            }
        }

        $data['list'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }


    public function detail_hou_gongzhi(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['zhi.deleted_user','=', 0];
        $where[] = ['zhi.comid','=',$this->userinfo['comid']];
        $where[] = ['zhi.isturn','=',1];
        $where[] = ['zhi.status','in',[0,1]];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['zhi.enginid','=', $param['enginid']];
        }
        if(isset($param['listid']) && !empty($param['listid'])){
            $where[] = ['list.id','=', $param['listid']];
        }

        if(isset($param['dealtype']) && !empty($param['dealtype'])){
            $where[] = ['deal.type','=', $param['dealtype']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['bill.sericnum|e.name|e.number|e.gofuer|list.fuer|zhi.flowuser','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['sericnum']) && !empty($param['sericnum'])){
            $where[] = ['bill.sericnum','like', '%'.$param['sericnum'].'%'];
        }
        if(isset($param['engin_name']) && !empty($param['engin_name'])){
            $where[] = ['e.name','like', '%'.$param['engin_name'].'%'];
        }
        if(isset($param['engin_number']) && !empty($param['engin_number'])){
            $where[] = ['e.number','like', '%'.$param['engin_number'].'%'];
        }
        if(isset($param['engin_state']) && !empty($param['engin_state'])){
            $where[] = ['e.state','=', $param['engin_state']];
        }
        if(isset($param['engin_gofuer']) && !empty($param['engin_gofuer'])){
            $where[] = ['e.gofuer','like', '%'.$param['engin_gofuer'].'%'];
        }
        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['zhi.flowuser','like', '%'.$param['flowuser'].'%'];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $created_atArr = explode(' - ', $param['created_at']);
            $where[] = ['zhi.created_at','between', [$created_atArr[0].' 00:00:00', $created_atArr[1].' 23:59:59']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['zhi.status','=', $param['status']];
        }
        if(isset($param['zhikuanlei']) && !empty($param['zhikuanlei'])){
            $where[] = ['list.zhikuanlei','=', $param['zhikuanlei']];
        }
        if(isset($param['incdaotype']) && !empty($param['incdaotype'])){
            $where[] = ['list.incdaotype','=', $param['incdaotype']];
        }
        if(isset($param['isfan']) && $param['isfan'] != ''){
            $where[] = ['list.isfan','=', $param['isfan']];
        }
        if(isset($param['fuer']) && !empty($param['fuer'])){
            $where[] = ['list.fuer','like', '%'.$param['fuer'].'%'];
        }

        if(isset($param['qutype']) && !empty($param['qutype'])){
            $where[] = ['qu.type','=', $param['qutype']];
        }

        $data['zhikuanlei'] = (new OptService())->get_base_info('cai_zhikuanlei');
        $data['is_zhikuanlei'] = isset($param['zhikuanlei']) ? $param['zhikuanlei'] : '';

        $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
        $data['is_incdaotype'] = isset($param['incdaotype']) ? $param['incdaotype'] : '';

        $data['engin_state'] = (new EnginService())->get_engin_state();
        $data['is_engin_state'] = isset($param['engin_state']) ? $param['engin_state'] : '';

        $list = [];
        $data['data'] = HouGongzhiListModel::alias('list')
            ->field('zhi.*,e.name as enginname,e.number as enginnumber,e.gofuer,e.state,bill.sericnum,cai.name as cainame,
            list.id as listid,list.zhikuanlei,list.fuer,list.money as listmoney,list.explain as listexplain,list.incdaotype,
            list.banknum,list.bankname,list.difee,list.fuer,list.banknum,list.bankname,list.caiid,list.isfan,list.fanshuo,qu.type as qutype')
            ->join('hou_gongzhi zhi','zhi.id=list.gongzhiid')
            ->join('flow_bill bill','bill.id=zhi.billid')
            ->join('engin e','e.id=zhi.enginid')
            ->leftJoin('dealcom deal','deal.name=list.fuer')
            ->leftJoin('contract_cai cai','cai.id=list.caiid')
            ->leftJoin('hou_gongzhi_qu qu','qu.gongzhiid=zhi.id and qu.gongzhilistid=list.id')
            ->where($where)
            ->group('list.id DESC')
            ->order('zhi.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['zhikuanlei_name'] = (new OptService())->get_base_showname('cai_zhikuanlei', $item['zhikuanlei']);
                $item['incdaotype_name'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);

                $item['yifufee'] = HouGongzhiGuanModel::alias('guan')
                    ->field('list.*')
                    ->join('hou_gongzhi zhi','zhi.id=guan.gongzhiid')
                    ->join('piao_shou piao','piao.id=guan.piaoid')
                    ->where(['zhi.comid'=>$this->userinfo['comid'],'zhi.deleted_user'=>0,'zhi.isturn'=>1,'zhi.status'=>1])
                    ->where(['piao.comid'=>$this->userinfo['comid'],'piao.deleted_user'=>0,'piao.isturn'=>1,'piao.status'=>1])
                    ->where(['guan.gongzhilistid'=>$item['listid']])
                    ->sum('difee');

                if($item['yifufee'] <= 0){
                    $item['statename'] = '未收票';
                }else{
                    if($item['yifufee'] < $item['listmoney']){
                        $item['statename'] = '部分收票';
                    }else{
                        $item['statename'] = '已收票';
                    }
                }
                $item['daifufee'] = round($item['listmoney'] - $item['yifufee'], 2);



                //资金渠道及金额
                $item['moneyshuo'] = '';
                $quList = HouGongzhiQuModel::where(['gongzhiid'=>$item['id'],'gongzhilistid'=>$item['listid']])->order('type ASC')->select()->toArray();
                foreach ($quList as $kkk=>$vvv){
                    $kkknum = $kkk+1;
                    if($vvv['type'] == 1){
                        $item['moneyshuo'] .= $kkknum.'、'.'工程负责人预收款：'.$vvv['money'].'<br>';
                    }
                    if($vvv['type'] == 2){
                        $item['moneyshuo'] .= $kkknum.'、'.'[当前工程]'.$item['enginname'].'：'.$vvv['money'].'<br>';
                    }
                    if($vvv['type'] == 3){
                        $qienname = EnginModel::where(['id'=>$vvv['qienid']])->value('name');
                        $item['moneyshuo'] .= $kkknum.'、'.$qienname.'：'.$vvv['money'].'<br>';
                    }
                }

                return $item;
            })->toArray();

//        foreach ($info as &$item){
//            $item['zhikuanlei_name'] = (new OptService())->get_base_showname('cai_zhikuanlei', $item['zhikuanlei']);
//            $item['incdaotype_name'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);
//
//            $item['yifufee'] = HouGongzhiGuanModel::alias('guan')
//                ->field('list.*')
//                ->join('hou_gongzhi zhi','zhi.id=guan.gongzhiid')
//                ->join('piao_shou piao','piao.id=guan.piaoid')
//                ->where(['zhi.comid'=>$this->userinfo['comid'],'zhi.deleted_user'=>0,'zhi.isturn'=>1,'zhi.status'=>1])
//                ->where(['piao.comid'=>$this->userinfo['comid'],'piao.deleted_user'=>0,'piao.isturn'=>1,'piao.status'=>1])
//                ->where(['guan.gongzhilistid'=>$item['listid']])
//                ->sum('difee');
//
//            if($item['yifufee'] <= 0){
//                $item['statename'] = '未收票';
//            }else{
//                if($item['yifufee'] < $item['listmoney']){
//                    $item['statename'] = '部分收票';
//                }else{
//                    $item['statename'] = '已收票';
//                }
//            }
//            $item['daifufee'] = round($item['listmoney'] - $item['yifufee'], 2);
//
//
//
//            //资金渠道及金额
//            $item['moneyshuo'] = '';
//            $quList = HouGongzhiQuModel::where(['gongzhiid'=>$item['id'],'gongzhilistid'=>$item['listid']])->order('type ASC')->select()->toArray();
//            foreach ($quList as $kkk=>$vvv){
//                $kkknum = $kkk+1;
//                if($vvv['type'] == 1){
//                    $item['moneyshuo'] .= $kkknum.'、'.'工程负责人预收款：'.$vvv['money'].'<br>';
//                }
//                if($vvv['type'] == 2){
//                    $item['moneyshuo'] .= $kkknum.'、'.'[当前工程]'.$item['enginname'].'：'.$vvv['money'].'<br>';
//                }
//                if($vvv['type'] == 3){
//                    $qienname = EnginModel::where(['id'=>$vvv['qienid']])->value('name');
//                    $item['moneyshuo'] .= $kkknum.'、'.$qienname.'：'.$vvv['money'].'<br>';
//                }
//            }
//
//
//            if(isset($param['statename']) && !empty($param['statename'])){
//                if($param['statename'] == $item['statename']){
//                    $list[] = $item;
//                }
//            }else{
//                $list[] = $item;
//            }
//        }
//
//        $data['data'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function detail_hou_gongzhi3333(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['zhi.deleted_user','=', 0];
        $where[] = ['zhi.comid','=',$this->userinfo['comid']];
        $where[] = ['zhi.isturn','=',1];
        $where[] = ['zhi.status','in',[0,1]];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['zhi.enginid','=', $param['enginid']];
        }
        if(isset($param['listid']) && !empty($param['listid'])){
            $where[] = ['list.id','=', $param['listid']];
        }

        if(isset($param['dealtype']) && !empty($param['dealtype'])){
            $where[] = ['deal.type','=', $param['dealtype']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['bill.sericnum|e.name|e.number|e.gofuer|list.fuer|zhi.flowuser','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['sericnum']) && !empty($param['sericnum'])){
            $where[] = ['bill.sericnum','like', '%'.$param['sericnum'].'%'];
        }
        if(isset($param['engin_name']) && !empty($param['engin_name'])){
            $where[] = ['e.name','like', '%'.$param['engin_name'].'%'];
        }
        if(isset($param['engin_number']) && !empty($param['engin_number'])){
            $where[] = ['e.number','like', '%'.$param['engin_number'].'%'];
        }
        if(isset($param['engin_state']) && !empty($param['engin_state'])){
            $where[] = ['e.state','=', $param['engin_state']];
        }
        if(isset($param['engin_gofuer']) && !empty($param['engin_gofuer'])){
            $where[] = ['e.gofuer','like', '%'.$param['engin_gofuer'].'%'];
        }
        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['zhi.flowuser','like', '%'.$param['flowuser'].'%'];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $created_atArr = explode(' - ', $param['created_at']);
            $where[] = ['zhi.created_at','between', [$created_atArr[0].' 00:00:00', $created_atArr[1].' 23:59:59']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['zhi.status','=', $param['status']];
        }
        if(isset($param['zhikuanlei']) && !empty($param['zhikuanlei'])){
            $where[] = ['list.zhikuanlei','=', $param['zhikuanlei']];
        }
        if(isset($param['incdaotype']) && !empty($param['incdaotype'])){
            $where[] = ['list.incdaotype','=', $param['incdaotype']];
        }
        if(isset($param['isfan']) && $param['isfan'] != ''){
            $where[] = ['list.isfan','=', $param['isfan']];
        }
        if(isset($param['fuer']) && !empty($param['fuer'])){
            $where[] = ['list.fuer','like', '%'.$param['fuer'].'%'];
        }

        if(isset($param['qutype']) && !empty($param['qutype'])){
            $where[] = ['qu.type','=', $param['qutype']];
        }

        $data['zhikuanlei'] = (new OptService())->get_base_info('cai_zhikuanlei');
        $data['is_zhikuanlei'] = isset($param['zhikuanlei']) ? $param['zhikuanlei'] : '';

        $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
        $data['is_incdaotype'] = isset($param['incdaotype']) ? $param['incdaotype'] : '';

        $data['engin_state'] = (new EnginService())->get_engin_state();
        $data['is_engin_state'] = isset($param['engin_state']) ? $param['engin_state'] : '';

        $list = [];
        $info = HouGongzhiListModel::alias('list')
            ->field('zhi.*,e.name as enginname,e.number as enginnumber,e.gofuer,e.state,bill.sericnum,cai.name as cainame,
            list.id as listid,list.zhikuanlei,list.fuer,list.money as listmoney,list.explain as listexplain,list.incdaotype,
            list.banknum,list.bankname,list.difee,list.fuer,list.banknum,list.bankname,list.caiid,list.isfan,list.fanshuo,qu.type as qutype')
            ->join('hou_gongzhi zhi','zhi.id=list.gongzhiid')
            ->join('flow_bill bill','bill.id=zhi.billid')
            ->join('engin e','e.id=zhi.enginid')
            ->leftJoin('dealcom deal','deal.name=list.fuer')
            ->leftJoin('contract_cai cai','cai.id=list.caiid')
            ->leftJoin('hou_gongzhi_qu qu','qu.gongzhiid=zhi.id and qu.gongzhilistid=list.id')
            ->where($where)
            ->group('list.id DESC')
            ->order('zhi.id DESC')
            ->select()->toArray();
        foreach ($info as &$item){
            $item['zhikuanlei_name'] = (new OptService())->get_base_showname('cai_zhikuanlei', $item['zhikuanlei']);
            $item['incdaotype_name'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);

            $item['yifufee'] = HouGongzhiGuanModel::alias('guan')
                ->field('list.*')
                ->join('hou_gongzhi zhi','zhi.id=guan.gongzhiid')
                ->join('piao_shou piao','piao.id=guan.piaoid')
                ->where(['zhi.comid'=>$this->userinfo['comid'],'zhi.deleted_user'=>0,'zhi.isturn'=>1,'zhi.status'=>1])
                ->where(['piao.comid'=>$this->userinfo['comid'],'piao.deleted_user'=>0,'piao.isturn'=>1,'piao.status'=>1])
                ->where(['guan.gongzhilistid'=>$item['listid']])
                ->sum('difee');

            if($item['yifufee'] <= 0){
                $item['statename'] = '未收票';
            }else{
                if($item['yifufee'] < $item['listmoney']){
                    $item['statename'] = '部分收票';
                }else{
                    $item['statename'] = '已收票';
                }
            }
            $item['daifufee'] = round($item['listmoney'] - $item['yifufee'], 2);



            //资金渠道及金额
            $item['moneyshuo'] = '';
            $quList = HouGongzhiQuModel::where(['gongzhiid'=>$item['id'],'gongzhilistid'=>$item['listid']])->order('type ASC')->select()->toArray();
            foreach ($quList as $kkk=>$vvv){
                $kkknum = $kkk+1;
                if($vvv['type'] == 1){
                    $item['moneyshuo'] .= $kkknum.'、'.'工程负责人预收款：'.$vvv['money'].'<br>';
                }
                if($vvv['type'] == 2){
                    $item['moneyshuo'] .= $kkknum.'、'.'[当前工程]'.$item['enginname'].'：'.$vvv['money'].'<br>';
                }
                if($vvv['type'] == 3){
                    $qienname = EnginModel::where(['id'=>$vvv['qienid']])->value('name');
                    $item['moneyshuo'] .= $kkknum.'、'.$qienname.'：'.$vvv['money'].'<br>';
                }
            }


            if(isset($param['statename']) && !empty($param['statename'])){
                if($param['statename'] == $item['statename']){
                    $list[] = $item;
                }
            }else{
                $list[] = $item;
            }
        }

        $data['data'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function detail_hou_gongzhi222(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['zhi.deleted_user','=', 0];
        $where[] = ['zhi.comid','=',$this->userinfo['comid']];
        $where[] = ['zhi.isturn','=',1];
        $where[] = ['zhi.status','in',[0,1]];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['zhi.enginid','=', $param['enginid']];
        }
        if(isset($param['dealtype']) && !empty($param['dealtype'])){
            $where[] = ['deal.type','=', $param['dealtype']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['bill.sericnum|e.name|e.number|e.gofuer|list.fuer|zhi.flowuser','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['sericnum']) && !empty($param['sericnum'])){
            $where[] = ['bill.sericnum','like', '%'.$param['sericnum'].'%'];
        }
        if(isset($param['engin_name']) && !empty($param['engin_name'])){
            $where[] = ['e.name','like', '%'.$param['engin_name'].'%'];
        }
        if(isset($param['engin_number']) && !empty($param['engin_number'])){
            $where[] = ['e.number','like', '%'.$param['engin_number'].'%'];
        }
        if(isset($param['engin_state']) && !empty($param['engin_state'])){
            $where[] = ['e.state','=', $param['engin_state']];
        }
        if(isset($param['engin_gofuer']) && !empty($param['engin_gofuer'])){
            $where[] = ['e.gofuer','like', '%'.$param['engin_gofuer'].'%'];
        }
        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['zhi.flowuser','like', '%'.$param['flowuser'].'%'];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $created_atArr = explode(' - ', $param['created_at']);
            $where[] = ['zhi.created_at','between', [$created_atArr[0].' 00:00:00', $created_atArr[1].' 23:59:59']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['zhi.status','=', $param['status']];
        }
        if(isset($param['zhikuanlei']) && !empty($param['zhikuanlei'])){
            $where[] = ['list.zhikuanlei','=', $param['zhikuanlei']];
        }
        if(isset($param['incdaotype']) && !empty($param['incdaotype'])){
            $where[] = ['list.incdaotype','=', $param['incdaotype']];
        }
        if(isset($param['isfan']) && $param['isfan'] != ''){
            $where[] = ['list.isfan','=', $param['isfan']];
        }
        if(isset($param['fuer']) && !empty($param['fuer'])){
            $where[] = ['list.fuer','like', '%'.$param['fuer'].'%'];
        }

        if(isset($param['qutype']) && !empty($param['qutype'])){
            $where[] = ['qu.type','=', $param['qutype']];
        }

        $data['zhikuanlei'] = (new OptService())->get_base_info('cai_zhikuanlei');
        $data['is_zhikuanlei'] = isset($param['zhikuanlei']) ? $param['zhikuanlei'] : '';

        $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
        $data['is_incdaotype'] = isset($param['incdaotype']) ? $param['incdaotype'] : '';

        $data['engin_state'] = (new EnginService())->get_engin_state();
        $data['is_engin_state'] = isset($param['engin_state']) ? $param['engin_state'] : '';

        $data['data'] = HouGongzhiListModel::alias('list')
            ->field('zhi.*,e.name as enginname,e.number as enginnumber,e.gofuer,e.state,bill.sericnum,cai.name as cainame,
            list.id as listid,list.zhikuanlei,list.fuer,list.money as listmoney,list.explain as listexplain,list.incdaotype,
            list.banknum,list.bankname,list.difee,list.fuer,list.banknum,list.bankname,list.caiid,list.isfan,list.fanshuo,qu.type as qutype')
            ->join('hou_gongzhi zhi','zhi.id=list.gongzhiid')
            ->join('flow_bill bill','bill.id=zhi.billid')
            ->join('engin e','e.id=zhi.enginid')
            ->leftJoin('dealcom deal','deal.name=list.fuer')
            ->leftJoin('contract_cai cai','cai.id=list.caiid')
            ->leftJoin('hou_gongzhi_qu qu','qu.gongzhiid=zhi.id and qu.gongzhilistid=list.id')
            ->where($where)
            ->group('list.id DESC')
            ->order('zhi.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['zhikuanlei_name'] = (new OptService())->get_base_showname('cai_zhikuanlei', $item['zhikuanlei']);
                $item['incdaotype_name'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);

                $item['yifufee'] = HouGongzhiGuanModel::alias('guan')
                    ->field('list.*')
                    ->join('hou_gongzhi zhi','zhi.id=guan.gongzhiid')
                    ->join('piao_shou piao','piao.id=guan.piaoid')
                    ->where(['zhi.comid'=>$this->userinfo['comid'],'zhi.deleted_user'=>0,'zhi.isturn'=>1,'zhi.status'=>1])
                    ->where(['piao.comid'=>$this->userinfo['comid'],'piao.deleted_user'=>0,'piao.isturn'=>1,'piao.status'=>1])
                    ->where(['guan.gongzhilistid'=>$item['listid']])
                    ->sum('difee');

                if($item['yifufee'] <= 0){
                    $item['statename'] = '未收票';
                }else{
                    if($item['yifufee'] < $item['listmoney']){
                        $item['statename'] = '部分收票';
                    }else{
                        $item['statename'] = '已收票';
                    }
                }
                $item['daifufee'] = round($item['listmoney'] - $item['yifufee'], 2);



                //资金渠道及金额
                $item['moneyshuo'] = '';
                $quList = HouGongzhiQuModel::where(['gongzhiid'=>$item['id'],'gongzhilistid'=>$item['listid']])->order('type ASC')->select()->toArray();
                foreach ($quList as $kkk=>$vvv){
                    $kkknum = $kkk+1;
                    if($vvv['type'] == 1){
                        $item['moneyshuo'] .= $kkknum.'、'.'工程负责人预收款：'.$vvv['money'].'<br>';
                    }
                    if($vvv['type'] == 2){
                        $item['moneyshuo'] .= $kkknum.'、'.'[当前工程]'.$item['enginname'].'：'.$vvv['money'].'<br>';
                    }
                    if($vvv['type'] == 3){
                        $qienname = EnginModel::where(['id'=>$vvv['qienid']])->value('name');
                        $item['moneyshuo'] .= $kkknum.'、'.$qienname.'：'.$vvv['money'].'<br>';
                    }
                }

                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_hou_gongzhi(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['zhi.deleted_user','=', 0];
        $where[] = ['zhi.comid','=',$this->userinfo['comid']];
        $where[] = ['zhi.isturn','=',1];
        $where[] = ['zhi.status','in',[0,1]];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['zhi.enginid','=', $param['enginid']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|e.number|e.gofuer|zhi.flowuser|zhi.zhai','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['engin_name']) && !empty($param['engin_name'])){
            $where[] = ['e.name','like', '%'.$param['engin_name'].'%'];
        }
        if(isset($param['engin_number']) && !empty($param['engin_number'])){
            $where[] = ['e.number','like', '%'.$param['engin_number'].'%'];
        }
        if(isset($param['engin_state']) && !empty($param['engin_state'])){
            $where[] = ['e.state','=', $param['engin_state']];
        }
        if(isset($param['fulei']) && !empty($param['fulei'])){
            $where[] = ['zhi.fulei','=', $param['fulei']];
        }

        if(isset($param['engin_gofuer']) && !empty($param['engin_gofuer'])){
            $where[] = ['e.gofuer','like', '%'.$param['engin_gofuer'].'%'];
        }
        if(isset($param['gui_unit']) && !empty($param['gui_unit'])){
            $where[] = ['gui_unit.id','=', $param['gui_unit']];
        }
        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['zhi.flowuser','like', '%'.$param['flowuser'].'%'];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $created_atArr = explode(' - ', $param['created_at']);
            $where[] = ['zhi.created_at','between', [$created_atArr[0].' 00:00:00', $created_atArr[1].' 23:59:59']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['zhi.status','=', $param['status']];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['zhi.zhai','like', '%'.$param['zhai'].'%'];
        }

        $data['engin_state'] = (new EnginService())->get_engin_state();
        $data['is_engin_state'] = isset($param['engin_state']) ? $param['engin_state'] : '';

        $data['fulei'] = (new HouService())->get_hou_gongzhi_fulei();
        $data['is_fulei'] = isset($param['fulei']) ? $param['fulei'] : '';

        $data['gui_unit'] = (new UserService())->get_chaninfo_all();
        $data['is_gui_unit'] = isset($param['gui_unit']) ? $param['gui_unit'] : '';


        $data['data'] = HouGongzhiModel::alias('zhi')
            ->field('zhi.*,e.name as enginname,gui_unit.name as gui_unit_name')
            ->join('engin e','e.id=zhi.enginid')
            ->leftJoin('dept gui_unit','gui_unit.id=e.gui_unit')
            ->where($where)
            ->where((new MenuService())->get_miss_where('hou_gongzhi', 'zhi'))
            ->order('zhi.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){

                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('hou_gongzhi');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function fan_hou_gongzhi(Request $request){
        if($request->isPost()){
            $param = $request->post();

            Db::startTrans();
            try {
                HouGongzhiListModel::where('id','in',$param['ids'])->update(['isfan'=>$param['isfan'],'fanshuo'=>$param['fanshuo']]);
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function del_hou_gongzhi(Request $request){
        $param = $request->post();
        $data = HouGongzhiModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            HouGongzhiModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'hou_gongzhi', $data['id'], '工程支付');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_hou_gongzhi(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = HouGongzhiModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);

        $list = HouGongzhiListModel::where(['gongzhiid'=>$data['data']['id']])->select()->toArray();
        foreach ($list as &$item){
            $item['incdaotype_name'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);
            $item['zhikuanlei_name'] = (new OptService())->get_base_showname('cai_zhikuanlei', $item['zhikuanlei']);
            $item['caiid_name'] = ContractCaiModel::where(['id'=>$item['caiid']])->value('name');

            $item['gongzhifee'] = HouGongzhiListModel::alias('list')
                ->join('hou_gongzhi hou','hou.id=list.gongzhiid')
                ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                ->where(['hou.enginid'=>$data['data']['enginid'],'list.fuer'=>$item['fuer']])
                ->sum('list.money');
            $item['piaoshoufee'] = PiaoShouListModel::alias('list')
                ->join('piao_shou shou','shou.id=list.shouid')
                ->where(['shou.comid'=>$this->userinfo['comid'],'shou.deleted_user'=>0,'shou.isturn'=>1,'shou.status'=>1])
                ->where(['shou.enginid'=>$data['data']['enginid'],'list.xiaoer'=>$item['fuer']])
                ->sum('list.money');

            $item['concaimoney'] = (new HouService())->get_contract_cai_final_money($data['data']['enginid'],$item['fuer']);

            //资金渠道及金额
            $item['moneyshuo'] = '';
            $item['moneyshuo_mob'] = '';
            $quList = HouGongzhiQuModel::where(['gongzhiid'=>$item['gongzhiid'],'gongzhilistid'=>$item['id']])->order('type ASC')->select()->toArray();
            foreach ($quList as $kkk=>$vvv){
                $kkknum = $kkk+1;
                if($vvv['type'] == 1){
                    $item['moneyshuo'] .= $kkknum.'、'.'工程负责人预收款：'.$vvv['money'].'<br>';
                    $item['moneyshuo_mob'] .= $kkknum.'、'.'工程负责人预收款：'.$vvv['money'].' ';
                }
                if($vvv['type'] == 2){
                    $item['moneyshuo'] .= $kkknum.'、'.'[当前工程]'.$data['engininfo']['name'].'：'.$vvv['money'].'<br>';
                    $item['moneyshuo_mob'] .= $kkknum.'、'.'[当前工程]'.$data['engininfo']['name'].'：'.$vvv['money'].' ';
                }
                if($vvv['type'] == 3){
                    $qienname = EnginModel::where(['id'=>$vvv['qienid']])->value('name');
                    $item['moneyshuo'] .= $kkknum.'、'.$qienname.'：'.$vvv['money'].'<br>';
                    $item['moneyshuo_mob'] .= $kkknum.'、'.$qienname.'：'.$vvv['money'].' ';
                }
            }

            $item['kuaninfo'] = (new FinanceService())->get_acckuan_info_bybillid($data['data']['billid'], $item['id']);
        }
        $data['list'] = $list;

        $data['last_kou_list'] = (new HouService())->get_engin_last_kou_list($data['data']['enginid']);

        $data['files'] = (new OptService())->get_file('hou_gongzhi', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }
    public function list_piao_shou_gongzhi_guan(Request $request){
        $enginid = $request->post('enginid', 0, 'int');
        $fuer = $request->post('fuer', '', 'string');
        if(!$fuer){
            return outputJson(2000,lang('code_2000'),['list'=>[]]);
        }

        $list = [];
        $chuList = PiaoShouListModel::alias('list')
            ->field('list.*')
            ->join('piao_shou hou','hou.id=list.shouid')
            ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
            ->where(['hou.enginid'=>$enginid,'list.xiaoer'=>$fuer])
            ->select()->toArray();

        foreach ($chuList as $item){
            $item['piaoid'] = $item['shouid'];
            $item['piaolistid'] = $item['id'];
            $item['qianpiaotype_name'] = (new OptService())->get_base_showname('cai_qianpiaotype', $item['qianpiaotype']);

            $item['yifufee'] = HouGongzhiGuanModel::alias('guan')
                ->field('list.*')
                ->join('hou_gongzhi zhi','zhi.id=guan.gongzhiid')
                ->join('piao_shou piao','piao.id=guan.piaoid')
                ->where(['zhi.comid'=>$this->userinfo['comid'],'zhi.deleted_user'=>0,'zhi.isturn'=>1,'zhi.status'=>1])
                ->where(['piao.comid'=>$this->userinfo['comid'],'piao.deleted_user'=>0,'piao.isturn'=>1,'piao.status'=>1])
                ->where(['guan.piaolistid'=>$item['id']])
                ->sum('difee');
            if($item['yifufee'] <= 0){
                $item['statename'] = '未付款';
            }else{
                if($item['yifufee'] < $item['money']){
                    $item['statename'] = '部分付款';
                }else{
                    continue;
                }
            }
            $item['daifufee'] = round($item['money'] - $item['yifufee'], 2);
            $list[] = $item;
        }

        $data['list'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_gongzhi_qu(Request $request){
        $enginid = $request->post('enginid', 0, 'int');
        $engininfo = EnginModel::where(['id'=>$enginid])->find();
        if(!$engininfo){
            return outputJson(2000,lang('code_2000'),['list'=>[]]);
        }

        $list = [];

        $hougongyumoney = (new HouService())->get_hou_gongyu_money($engininfo['gofuer']);
        $hougongyukoufee = (new HouService())->get_hou_gongyu_koufee($engininfo['gofuer']);
        $hougongzhimoneyqugoer = (new HouService())->get_hou_gongzhi_money_qugoer($engininfo['gofuer']);
        $yujiedongkoufee = HouKouQifeiModel::alias('list')
            ->join('hou_gongyu hou','hou.id=list.zhuid')
            ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
            ->where(['list.type'=>2, 'hou.gofuer'=>$engininfo['gofuer'],'isdong'=>1])
            ->sum('list.koufee');
        $hougongshuimoneygoquan = (new HouService())->get_hou_gongshui_money_goquan($engininfo['gofuer']);
        $gongyufee = round($hougongyumoney+$hougongshuimoneygoquan-$hougongyukoufee-$hougongzhimoneyqugoer+$yujiedongkoufee,2);
        $list[] = ['type'=>1, 'name'=>'工程负责人预收款', 'banlance'=>$gongyufee,'qienid'=>0];


        $hougonghuimoney = (new HouService())->get_hou_gonghui_money($engininfo['id']);
        $hougongzhimoneyquben = (new HouService())->get_hou_gongzhi_money_quben($engininfo['id']);
//        $hougonghuikoufee = (new HouService())->get_hou_gonghui_koufee($engininfo['id']);
        $hougonghuikoufee = HouGonghuiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['enginid'=>$engininfo['id']])->sum('koufee');
        $jiedongkoufee = HouKouQifeiModel::alias('list')
            ->join('hou_gonghui hou','hou.id=list.zhuid')
            ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
            ->where(['list.type'=>1, 'list.enginid'=>$engininfo['id'],'isdong'=>1])
            ->sum('list.koufee');

        $jiedongkoufee += HouKouQifeiModel::alias('list')
            ->join('hou_gongyu hou','hou.id=list.zhuid')
            ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
            ->where(['list.type'=>2, 'list.enginid'=>$engininfo['id'],'isdong'=>1])
            ->sum('list.koufee');
        $benfee = round($hougonghuimoney - $hougongzhimoneyquben - $hougonghuikoufee + $jiedongkoufee, 2);
        $list[] = ['type'=>2, 'name'=>'[当前工程]'.$engininfo['name'], 'banlance'=>$benfee,'qienid'=>0];



        $qixiang = EnginModel::field('id,name')->where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['gofuer'=>$engininfo['gofuer']])->where('id','neq',$engininfo['id'])->select()->toArray();
        foreach ($qixiang as $item){
            $item['hougonghuimoney'] = (new HouService())->get_hou_gonghui_money($item['id']);
            $item['hougongzhimoneyquben'] = (new HouService())->get_hou_gongzhi_money_quben($item['id']);
            $item['hougonghuikoufee'] = (new HouService())->get_hou_gonghui_koufee($item['id']);
            $yufee = round($item['hougonghuimoney'] - $item['hougongzhimoneyquben'] - $item['hougonghuikoufee'], 2);
            $list[] = ['type'=>3, 'name'=>$item['name'], 'banlance'=>$yufee,'qienid'=>$item['id']];
        }

        $data['list'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }

    public function list_hou_gonghui(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['hui.deleted_user','=', 0];
        $where[] = ['hui.comid','=',$this->userinfo['comid']];
        $where[] = ['hui.isturn','=',1];
        $where[] = ['hui.status','in',[0,1]];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['hui.enginid','=', $param['enginid']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|e.number|e.gofuer|hui.flowuser|hui.zhai','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['engin_name']) && !empty($param['engin_name'])){
            $where[] = ['e.name','like', '%'.$param['engin_name'].'%'];
        }
        if(isset($param['engin_number']) && !empty($param['engin_number'])){
            $where[] = ['e.number','like', '%'.$param['engin_number'].'%'];
        }
        if(isset($param['engin_state']) && !empty($param['engin_state'])){
            $where[] = ['e.state','=', $param['engin_state']];
        }
        if(isset($param['engin_gofuer']) && !empty($param['engin_gofuer'])){
            $where[] = ['e.gofuer','like', '%'.$param['engin_gofuer'].'%'];
        }
        if(isset($param['gui_unit']) && !empty($param['gui_unit'])){
            $where[] = ['gui_unit.id','=', $param['gui_unit']];
        }
        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['hui.flowuser','like', '%'.$param['flowuser'].'%'];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $created_atArr = explode(' - ', $param['created_at']);
            $where[] = ['hui.created_at','between', [$created_atArr[0].' 00:00:00', $created_atArr[1].' 23:59:59']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['hui.status','=', $param['status']];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['hui.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['shougonglei']) && !empty($param['shougonglei'])){
            $where[] = ['hui.shougonglei','=', $param['shougonglei']];
        }
        if(isset($param['incdaotype']) && !empty($param['incdaotype'])){
            $where[] = ['hui.incdaotype','=', $param['incdaotype']];
        }
        if(isset($param['gonghelei']) && !empty($param['gonghelei'])){
            $where[] = ['e.gonghelei','=', $param['gonghelei']];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['hui.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['iskou']) && !empty($param['iskou'])){
            $where[] = ['hui.iskou','=', $param['iskou']];
        }

        $data['engin_state'] = (new EnginService())->get_engin_state();
        $data['is_engin_state'] = isset($param['engin_state']) ? $param['engin_state'] : '';

        $data['gui_unit'] = (new UserService())->get_chaninfo_all();
        $data['is_gui_unit'] = isset($param['gui_unit']) ? $param['gui_unit'] : '';

        $data['shougonglei'] = (new OptService())->get_base_info('cai_shougonglei');
        $data['is_shougonglei'] = isset($param['shougonglei']) ? $param['shougonglei'] : '';

        $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
        $data['is_incdaotype'] = isset($param['incdaotype']) ? $param['incdaotype'] : '';

        $data['gonghelei'] = (new OptService())->get_base_info('engin_gonghelei');
        $data['is_gonghelei'] = isset($param['gonghelei']) ? $param['gonghelei'] : '';


        $data['data'] = HouGonghuiModel::alias('hui')
            ->field('hui.*,e.name as enginname,e.gonghelei,acc.name as accname,acc.accnum,gui_unit.name as gui_unit_name')
            ->join('engin e','e.id=hui.enginid')
            ->leftJoin('dept gui_unit','gui_unit.id=e.gui_unit')
            ->leftJoin('account acc','acc.id=hui.accid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('hou_gonghui', 'hui'))
            ->order('hui.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['shougonglei_name'] = (new OptService())->get_base_showname('cai_shougonglei', $item['shougonglei']);
                $item['incdaotype_name'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);
                $item['gonghelei_name'] = (new OptService())->get_base_showname('engin_gonghelei', $item['gonghelei']);
                $item['accid_name'] = $item['accname'].'('.$item['accnum'].')';
                $item['koulei_name'] = (new HouService())->get_hou_koulei_name($item['koulei']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('hou_gonghui');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function check_hou_gonghui(Request $request){
        $id = $request->param('id',0,'int');
        $huiinfo = HouGonghuiModel::where(['id'=>$id])->find();
        if(!$huiinfo){
            return outputJson(4000, lang('code_4000'));
        }

        $result = false;
        if($huiinfo['iskou'] == '是' && !$huiinfo['koulei']){
            $result = false;
        }else{
            $result = true;
        }
        $data['result'] = $result;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function kou_hou_gonghui(Request $request){
        if($request->isPost()){
            $param = $request->post();


            if(!$param['koulei']){
                return outputJson(4000, '选择本次收取款项类型');
            }


            foreach ($param as $k=>$v){
                if(strpos($k,'houkoulei') !== false){
                    unset($param[$k]);
                }
            }



            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $kouleiArr = explode(',',$param['koulei']);

            $fuwu_list = array_filter($param['fuwu_list']);
            unset($param['fuwu_list']);
            if(in_array('fuwu',$kouleiArr)){
                if(count($fuwu_list) < 1){
                    return outputJson(4000, '请输入收取明细-待收取劳务分包服务费');
                }
            }else{
                $fuwu_list = [];
            }

            $shuifei_list = array_filter($param['shuifei_list']);
            unset($param['shuifei_list']);
            if(in_array('shuifei',$kouleiArr)){
                if(count($shuifei_list) < 1){
                    return outputJson(4000, '请输入收取明细-待收取税款(含利息)');
                }
            }else{
                $shuifei_list = [];
            }



            $gongjie_list = array_filter($param['gongjie_list']);
            unset($param['gongjie_list']);
            if(in_array('gongjie',$kouleiArr)){
                if(count($gongjie_list) < 1){
                    return outputJson(4000, '请输入收取明细-待收取工程负责人借款(含利息)');
                }
            }else{
                $gongjie_list = [];
            }
            $kuaifei_list = array_filter($param['kuaifei_list']);
            unset($param['kuaifei_list']);
            if(in_array('kuaifei',$kouleiArr)){
                if(count($kuaifei_list) < 1){
                    return outputJson(4000, '请输入收取明细-待收取快递费');
                }
            }else{
                $kuaifei_list = [];
            }

            $qifei_list = array_filter($param['qifei_list']);
            unset($param['qifei_list']);
            if(in_array('qifei',$kouleiArr)){
                if(count($qifei_list) < 1){
                    return outputJson(4000, '请输入收取明细-直接收取');
                }
            }else{
                $qifei_list = [];
            }



            Db::startTrans();
            try {
                HouGonghuiModel::update($param);


                HouKouFuwuModel::where(['zhuid'=>$param['id'],'type'=>1])->delete();
                foreach($fuwu_list as $vv){
                    $vv['type'] = 1;
                    $vv['zhuid'] = $param['id'];
                    HouKouFuwuModel::insert($vv);
                }

                HouKouShuifeiModel::where(['zhuid'=>$param['id'],'type'=>1])->delete();
                foreach($shuifei_list as $vv){
                    $vv['type'] = 1;
                    $vv['zhuid'] = $param['id'];
                    HouKouShuifeiModel::insert($vv);
                }

                HouKouGongjieModel::where(['zhuid'=>$param['id'],'type'=>1])->delete();
                foreach($gongjie_list as $vv){
                    $vv['type'] = 1;
                    $vv['zhuid'] = $param['id'];
                    HouKouGongjieModel::insert($vv);
                }

                HouKouKuaifeiModel::where(['zhuid'=>$param['id'],'type'=>1])->delete();
                foreach($kuaifei_list as $vv){
                    $vv['type'] = 1;
                    $vv['zhuid'] = $param['id'];
                    if($vv['kuaitype'] == 1){
                        $vv['enginid'] = KuaiJiModel::where(['id'=>$vv['kuaiid']])->value('enginid');
                    }else{
                        $vv['enginid'] = KuaiShouModel::where(['id'=>$vv['kuaiid']])->value('enginid');
                    }
                    HouKouKuaifeiModel::insert($vv);
                }

                HouKouQifeiModel::where(['zhuid'=>$param['id'],'type'=>1])->delete();
                foreach($qifei_list as $vv){
                    $vv['type'] = 1;
                    $vv['zhuid'] = $param['id'];
                    HouKouQifeiModel::insert($vv);
                }

                (new OptService())->relation_file($files,'hou_gonghui',$param['id']);
                (new OptService())->add_flow_log('编辑费用收取', 'hou_gonghui', $param['id']);
                (new OptService())->add_log_list('编辑费用收取', 'hou_gonghui', $param['id'], '工程回款');

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = HouGonghuiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
            $data['account'] = AccountModel::get_info_by_pk($data['data']['accid']);

            $list = HouGonghuiListModel::where(['gonghuiid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $piaoinfo = PiaoKaiListModel::alias('list')
                    ->field('list.*,e.name as enginname,kai.shouer,kai.qianpiaotype,kai.dt')
                    ->join('piao_kai kai','kai.id=list.kaiid')
                    ->join('engin e','e.id=kai.enginid')
                    ->where(['list.id'=>$item['piaoid']])->find();

                $item['nums'] = $piaoinfo['nums'];
                $item['shouer'] = $piaoinfo['shouer'];
                $item['qianpiaotype_name'] = (new OptService())->get_base_showname('cai_qianpiaotype', $piaoinfo['qianpiaotype']);
                $item['money'] = $piaoinfo['money'];
                $item['yihuikuan'] = HouGonghuiListModel::alias('list')
                    ->join('hou_gonghui hui','hui.id=list.gonghuiid')
                    ->where(['hui.comid'=>$this->userinfo['comid'],'hui.deleted_user'=>0,'hui.isturn'=>1,'hui.status'=>1])
                    ->where(['list.piaoid'=>$item['piaoid']])
                    ->sum('list.xiaofee');
                $item['daihuikuan'] = round($item['money'] - $item['yihuikuan'], 2);
            }
            $data['list'] = $list;


            if($data['data']['iskou'] == '是'){
                $data['data']['kouleiArr'] = explode(',', $data['data']['koulei']);

                $fuwu_list = HouKouFuwuModel::where(['type'=>1,'zhuid'=>$data['data']['id']])->select()->toArray();
                foreach ($fuwu_list as &$vvfuwu){
                    $vvfuwu['name'] = EnginModel::where(['id'=>$vvfuwu['enginid']])->value('name');

                    $vvfuwu['shimoney'] = (new EnginService())->get_contract_shi_money($vvfuwu['enginid']);
                    $vvfuwu['shijiemoney'] = (new EnginService())->get_contract_shi_jie_money($vvfuwu['enginid']);

                    $vvfuwu['ratefu'] = ContractNeiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
                        ->where(['enginid'=>$vvfuwu['enginid']])->order('id DESC')->value('ratefu');

                    $vvfuwu['yingkoufee'] = 0;
                    if($vvfuwu['shijiemoney']){
                        $vvfuwu['yingkoufee'] = round($vvfuwu['shijiemoney']*$vvfuwu['ratefu']/100, 2);
                    }else{
                        $vvfuwu['yingkoufee'] = round($vvfuwu['shimoney']*$vvfuwu['ratefu']/100, 2);
                    }

                    $vvfuwu['yikoufee'] = HouKouFuwuModel::alias('list')
                        ->join('hou_gonghui hou','hou.id=list.zhuid')
                        ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                        ->where(['list.type'=>1,'list.enginid'=>$vvfuwu['enginid']])
                        ->sum('list.koufee');
                    $vvfuwu['yikoufee'] += HouKouFuwuModel::alias('list')
                        ->join('hou_gongyu hou','hou.id=list.zhuid')
                        ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                        ->where(['list.type'=>2,'list.enginid'=>$vvfuwu['enginid']])
                        ->sum('list.koufee');

                    $vvfuwu['daikoufee'] = round($vvfuwu['yingkoufee'] - $vvfuwu['yikoufee'], 2);
                    $vvfuwu['daikoufee'] = $vvfuwu['daikoufee']  > 0 ? $vvfuwu['daikoufee'] : 0;
                }
                $data['fuwu_list'] = $fuwu_list;


                $shuifei_list = HouKouShuifeiModel::where(['type'=>1,'zhuid'=>$data['data']['id']])->select()->toArray();
                foreach ($shuifei_list as &$vvshuifei){
                    $shuiinfo = EnginShuiModel::where(['id'=>$vvshuifei['shuiid']])->find();
                    $piaoinfo = PiaokaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
                        ->where(['enginid'=>$shuiinfo['enginid'],'shuiid'=>$vvshuifei['shuiid']])->find();

                    $vvshuifei['name'] = $shuiinfo['name'];
                    $vvshuifei['enginname'] = EnginModel::where(['id'=>$shuiinfo['enginid']])->value('name');
                    $vvshuifei['shendt'] = date("Y-m-d", strtotime($shuiinfo['created_at']));
                    $vvshuifei['xiaofee'] = $shuiinfo['xiaofee'];
                    $vvshuifei['dt'] = $piaoinfo['dt'];
                    $vvshuifei['baofee'] = $shuiinfo['baofee'];
                }
                $data['shuifei_list'] = $shuifei_list;


                $gongjie_list = HouKouGongjieModel::where(['type'=>1,'zhuid'=>$data['data']['id']])->select()->toArray();
                foreach ($gongjie_list as &$vvgongjie){
                    $tiaoinfo = HouTiaoModel::where(['id'=>$vvgongjie['tiaoid']])->find();
                    $tiaoinfo = (new HouService())->get_tiaoinfo($tiaoinfo, $data['data']['dt']);
                    $vvgongjie['number'] = $tiaoinfo['number'];
                    $vvgongjie['chuer'] = $tiaoinfo['chuer'];
                    $vvgongjie['money'] = $tiaoinfo['money'];
                    $vvgongjie['daodt'] = $tiaoinfo['daodt'];
                    $vvgongjie['yueshu_zhan'] = $tiaoinfo['yueshu_zhan'];
                    $vvgongjie['daihuanbenfee'] = $tiaoinfo['daihuanbenfee'];
                    $vvgongjie['daihuanlifee'] = $tiaoinfo['daihuanlifee'];
                    $vvgongjie['daihuanallfee'] = $tiaoinfo['daihuanallfee'];
                }
                $data['gongjie_list'] = $gongjie_list;


                $kuaifei_list = HouKouKuaifeiModel::where(['type'=>1,'zhuid'=>$data['data']['id']])->select()->toArray();
                foreach ($kuaifei_list as &$vvkuaifei){
                    if($vvkuaifei['kuaitype'] == 1){
                        $jiinfo = KuaiJiModel::where(['id'=>$vvkuaifei['kuaiid']])->find();
                        $vvkuaifei['enginname'] = EnginModel::where(['id'=>$jiinfo['enginid']])->value('name');
                        $vvkuaifei['kuailei'] = '寄件';
                        $vvkuaifei['yunnum'] = $jiinfo['yunnum'];
                        $vvkuaifei['kuaidt'] = $jiinfo['jidt'];
                        $vvkuaifei['futype'] = $jiinfo['futype'];
                        $vvkuaifei['kuaifee'] = $jiinfo['kuaifee'];
                    }else{
                        $shouinfo = KuaiShouModel::where(['id'=>$vvkuaifei['kuaiid']])->find();
                        $vvkuaifei['enginname'] = EnginModel::where(['id'=>$shouinfo['enginid']])->value('name');
                        $vvkuaifei['kuailei'] = '收件';
                        $vvkuaifei['yunnum'] = $shouinfo['yunnum'];
                        $vvkuaifei['kuaidt'] = $shouinfo['jidt'];
                        $vvkuaifei['futype'] = $shouinfo['futype'];
                        $vvkuaifei['kuaifee'] = $shouinfo['kuaifee'];
                    }
                }
                $data['kuaifei_list'] = $kuaifei_list;


                $qifei_list = HouKouQifeiModel::where(['type'=>1,'zhuid'=>$data['data']['id']])->select()->toArray();
                foreach ($qifei_list as &$vvqifei){
                    $vvqifei['enginname'] = EnginModel::where(['id'=>$vvqifei['enginid']])->value('name');
                }
                $data['qifei_list'] = $qifei_list;
            }

            $data['data']['shougonglei_name'] = (new OptService())->get_base_showname('cai_shougonglei', $data['data']['shougonglei']);
            $data['data']['incdaotype_name'] = (new OptService())->get_base_showname('cai_income_incdaotype', $data['data']['incdaotype']);

            $houkoulei = (new OptService())->get_base_info('cai_houkoulei');
            $new_houkoulei = [];
            foreach ($houkoulei as $kkk=>$fff){
                if(!in_array($fff['name'], ['劳务分包服务费','税费利息','快递费','借款'])){
                    $new_houkoulei[] = $fff;
                }
            }
            $data['houkoulei'] = $new_houkoulei;

            $data['files'] = (new OptService())->get_file('hou_gonghui', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_hou_gonghui(Request $request){
        $param = $request->post();
        $data = HouGonghuiModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            HouGonghuiModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'hou_gonghui', $data['id'], '工程回款');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_hou_gonghui(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = HouGonghuiModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }
        $data['data']['shougonglei_name'] = (new OptService())->get_base_showname('cai_shougonglei', $data['data']['shougonglei']);
        $data['data']['incdaotype_name'] = (new OptService())->get_base_showname('cai_income_incdaotype', $data['data']['incdaotype']);

        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
        $data['account'] = AccountModel::get_info_by_pk($data['data']['accid']);

        $data['data']['xiaofee'] = HouGonghuiListModel::alias('list')
            ->join('hou_gonghui hui','hui.id=list.gonghuiid')
            ->where(['hui.comid'=>$this->userinfo['comid'],'hui.deleted_user'=>0,'hui.isturn'=>1,'hui.status'=>1])
            ->where(['list.gonghuiid'=>$data['data']['id']])
            ->sum('list.xiaofee');


        $list = HouGonghuiListModel::where(['gonghuiid'=>$data['data']['id']])->select()->toArray();
        foreach ($list as &$item){
            $piaoinfo = PiaoKaiListModel::alias('list')
                ->field('list.*,e.name as enginname,kai.shouer,kai.qianpiaotype,kai.dt')
                ->join('piao_kai kai','kai.id=list.kaiid')
                ->join('engin e','e.id=kai.enginid')
                ->where(['list.id'=>$item['piaoid']])->find();

            $item['nums'] = $piaoinfo['nums'];
            $item['shouer'] = $piaoinfo['shouer'];
            $item['qianpiaotype_name'] = (new OptService())->get_base_showname('cai_qianpiaotype', $piaoinfo['qianpiaotype']);
            $item['money'] = $piaoinfo['money'];
            $item['yihuikuan'] = HouGonghuiListModel::alias('list')
                ->join('hou_gonghui hui','hui.id=list.gonghuiid')
                ->where(['hui.comid'=>$this->userinfo['comid'],'hui.deleted_user'=>0,'hui.isturn'=>1,'hui.status'=>1])
                ->where(['list.piaoid'=>$item['piaoid']])
                ->sum('list.xiaofee');
            $item['daihuikuan'] = round($item['money'] - $item['yihuikuan'], 2);
        }
        $data['list'] = $list;


        if($data['data']['iskou'] == '是'){
            $data['data']['kouleiArr'] = explode(',', $data['data']['koulei']);
            $data['data']['koulei_name'] = (new HouService())->get_hou_koulei_name($data['data']['koulei']);

            $fuwu_list = HouKouFuwuModel::where(['type'=>1,'zhuid'=>$data['data']['id']])->select()->toArray();
            foreach ($fuwu_list as &$vvfuwu){
                $vvfuwu['houkoulei_name'] = (new OptService())->get_base_showname('cai_houkoulei', $vvfuwu['houkoulei']);
                $vvfuwu['name'] = EnginModel::where(['id'=>$vvfuwu['enginid']])->value('name');

                $vvfuwu['shimoney'] = (new EnginService())->get_contract_shi_money($vvfuwu['enginid']);
                $vvfuwu['shijiemoney'] = (new EnginService())->get_contract_shi_jie_money($vvfuwu['enginid']);

                $vvfuwu['ratefu'] = ContractNeiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
                    ->where(['enginid'=>$vvfuwu['enginid']])->order('id DESC')->value('ratefu');

                $vvfuwu['yingkoufee'] = 0;
                if($vvfuwu['shijiemoney']){
                    $vvfuwu['yingkoufee'] = round($vvfuwu['shijiemoney']*$vvfuwu['ratefu']/100, 2);
                }else{
                    $vvfuwu['yingkoufee'] = round($vvfuwu['shimoney']*$vvfuwu['ratefu']/100, 2);
                }

                $vvfuwu['yikoufee'] = HouKouFuwuModel::alias('list')
                    ->join('hou_gonghui hou','hou.id=list.zhuid')
                    ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                    ->where(['list.type'=>1,'list.enginid'=>$vvfuwu['enginid']])
                    ->sum('list.koufee');
                $vvfuwu['yikoufee'] += HouKouFuwuModel::alias('list')
                    ->join('hou_gongyu hou','hou.id=list.zhuid')
                    ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                    ->where(['list.type'=>2,'list.enginid'=>$vvfuwu['enginid']])
                    ->sum('list.koufee');
                $vvfuwu['daikoufee'] = round($vvfuwu['yingkoufee'] - $vvfuwu['yikoufee'], 2);
                $vvfuwu['daikoufee'] = $vvfuwu['daikoufee']  > 0 ? $vvfuwu['daikoufee'] : 0;
            }
            $data['fuwu_list'] = $fuwu_list;


            $shuifei_list = HouKouShuifeiModel::where(['type'=>1,'zhuid'=>$data['data']['id']])->select()->toArray();
            foreach ($shuifei_list as &$vvshuifei){
                $vvshuifei['houkoulei_name'] = (new OptService())->get_base_showname('cai_houkoulei', $vvshuifei['houkoulei']);
                $shuiinfo = EnginShuiModel::where(['id'=>$vvshuifei['shuiid']])->find();
                $piaoinfo = PiaokaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
                    ->where(['enginid'=>$shuiinfo['enginid'],'shuiid'=>$vvshuifei['shuiid'],'shuishou'=>2])->find();

                $vvshuifei['name'] = $shuiinfo['name'];
                $vvshuifei['enginname'] = EnginModel::where(['id'=>$shuiinfo['enginid']])->value('name');
                $vvshuifei['shendt'] = date("Y-m-d", strtotime($shuiinfo['created_at']));
                $vvshuifei['xiaofee'] = $shuiinfo['xiaofee'];
                $vvshuifei['dt'] = $piaoinfo['dt'];
                $vvshuifei['baofee'] = $shuiinfo['baofee'];
            }
            $data['shuifei_list'] = $shuifei_list;


            $gongjie_list = HouKouGongjieModel::where(['type'=>1,'zhuid'=>$data['data']['id']])->select()->toArray();
            foreach ($gongjie_list as &$vvgongjie){
                $vvgongjie['houkoulei_name'] = (new OptService())->get_base_showname('cai_houkoulei', $vvgongjie['houkoulei']);
                $tiaoinfo = HouTiaoModel::where(['id'=>$vvgongjie['tiaoid']])->find();
                $tiaoinfo = (new HouService())->get_tiaoinfo($tiaoinfo, $data['data']['dt']);
                $vvgongjie['number'] = $tiaoinfo['number'];
                $vvgongjie['chuer'] = $tiaoinfo['chuer'];
                $vvgongjie['money'] = $tiaoinfo['money'];
                $vvgongjie['daodt'] = $tiaoinfo['daodt'];
                $vvgongjie['yueshu_zhan'] = $tiaoinfo['yueshu_zhan'];
                $vvgongjie['daihuanbenfee'] = $tiaoinfo['daihuanbenfee'];
                $vvgongjie['daihuanlifee'] = $tiaoinfo['daihuanlifee'];
                $vvgongjie['daihuanallfee'] = $tiaoinfo['daihuanallfee'];
            }
            $data['gongjie_list'] = $gongjie_list;


            $kuaifei_list = HouKouKuaifeiModel::where(['type'=>1,'zhuid'=>$data['data']['id']])->select()->toArray();
            foreach ($kuaifei_list as &$vvkuaifei){
                $vvkuaifei['houkoulei_name'] = (new OptService())->get_base_showname('cai_houkoulei', $vvkuaifei['houkoulei']);
                if($vvkuaifei['kuaitype'] == 1){
                    $jiinfo = KuaiJiModel::where(['id'=>$vvkuaifei['kuaiid']])->find();
                    $vvkuaifei['enginname'] = EnginModel::where(['id'=>$jiinfo['enginid']])->value('name');
                    $vvkuaifei['kuailei'] = '寄件';
                    $vvkuaifei['yunnum'] = $jiinfo['yunnum'];
                    $vvkuaifei['kuaidt'] = $jiinfo['jidt'];
                    $vvkuaifei['futype'] = $jiinfo['futype'];
                    $vvkuaifei['kuaifee'] = $jiinfo['kuaifee'];
                }else{
                    $shouinfo = KuaiShouModel::where(['id'=>$vvkuaifei['kuaiid']])->find();
                    $vvkuaifei['enginname'] = EnginModel::where(['id'=>$shouinfo['enginid']])->value('name');
                    $vvkuaifei['kuailei'] = '收件';
                    $vvkuaifei['yunnum'] = $shouinfo['yunnum'];
                    $vvkuaifei['kuaidt'] = $shouinfo['jidt'];
                    $vvkuaifei['futype'] = $shouinfo['futype'];
                    $vvkuaifei['kuaifee'] = $shouinfo['kuaifee'];
                }
            }
            $data['kuaifei_list'] = $kuaifei_list;


            $qifei_list = HouKouQifeiModel::where(['type'=>1,'zhuid'=>$data['data']['id']])->select()->toArray();
            foreach ($qifei_list as &$vvqifei){
                $vvqifei['houkoulei_name'] = (new OptService())->get_base_showname('cai_houkoulei', $vvqifei['houkoulei']);
                $vvqifei['enginname'] = EnginModel::where(['id'=>$vvqifei['enginid']])->value('name');
            }
            $data['qifei_list'] = $qifei_list;
        }


        $data['files'] = (new OptService())->get_file('hou_gonghui', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function list_koulei_fuwu(Request $request){
        $enginid = $request->post('enginid', 0, 'int');
        $gofuer = $request->post('gofuer', '', 'string');

        $list = [];
        if($enginid){
            $gofuer = EnginModel::where(['id'=>$enginid])->value('gofuer');
        }
        $enginList = EnginModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['gofuer'=>$gofuer])->select()->toArray();

        foreach ($enginList as $item){
            $item['xuhao'] = $item['id'] == $enginid ? 1 : 0;
            $item['shimoney'] = (new EnginService())->get_contract_shi_money($item['id']);
            $item['shijiemoney'] = (new EnginService())->get_contract_shi_jie_money($item['id']);

            $item['ratefu'] = ContractNeiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
                ->where(['enginid'=>$item['id']])->order('id DESC')->value('ratefu');

            $item['yingkoufee'] = 0;
            if($item['shijiemoney']){
                $item['yingkoufee'] = round($item['shijiemoney']*$item['ratefu']/100, 2);
            }else{
                $item['yingkoufee'] = round($item['shimoney']*$item['ratefu']/100, 2);
            }

            $item['yikoufee'] = HouKouFuwuModel::alias('list')
                ->join('hou_gonghui hou','hou.id=list.zhuid')
                ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                ->where(['list.type'=>1,'list.enginid'=>$item['id']])
                ->sum('list.koufee');
            $item['yikoufee'] += HouKouFuwuModel::alias('list')
                ->join('hou_gongyu hou','hou.id=list.zhuid')
                ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                ->where(['list.type'=>2,'list.enginid'=>$item['id']])
                ->sum('list.koufee');
            $item['daikoufee'] = round($item['yingkoufee'] - $item['yikoufee'], 2);
            $item['daikoufee'] = $item['daikoufee']  > 0 ? $item['daikoufee'] : 0;

            $list[] = $item;
        }

        $xuhaoSort = array_column($list,'xuhao');
        array_multisort($xuhaoSort,SORT_DESC, $list);
        $data['data'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_koulei_shuifei(Request $request){
        $enginid = $request->post('enginid', 0, 'int');
        $daodt = $request->post('daodt', date("Y-m-d"), 'string');
        $gofuer = $request->post('gofuer', '', 'string');

        $list = [];
        if($enginid){
            $gofuer = EnginModel::where(['id'=>$enginid])->value('gofuer');
        }
        $enginList = EnginModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['gofuer'=>$gofuer])->select()->toArray();

        foreach ($enginList as $item){
            $shuiList = EnginShuiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
                ->where(['enginid'=>$item['id']])->select()->toArray();
            foreach ($shuiList as $vvv){
                $vvv['xuhao'] = $item['id'] == $enginid ? 1 : 0;
                $vvv['enginname'] = $item['name'];

                //是否在开票里面关联【并且开票日期不为空,】多个的话，取开票日期最小的数据
                $iskaipiao = PiaokaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
                    ->where(['enginid'=>$item['id'],'shuiid'=>$vvv['id'],'shuishou'=>2])
                    ->where("dt != '' and dt is not null and dt != '0000-00-00'")
                    ->order('dt ASC')->find();

                if(!$iskaipiao){
                    continue;
                }

                $yikouHui = HouKouShuifeiModel::alias('list')
                    ->join('hou_gonghui hou','hou.id=list.zhuid')
                    ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                    ->where(['list.type'=>1,'list.shuiid'=>$vvv['id']])
                    ->find();
                if($yikouHui){
                    continue;
                }
                $yikouYu = HouKouShuifeiModel::alias('list')
                    ->join('hou_gongyu hou','hou.id=list.zhuid')
                    ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                    ->where(['list.type'=>2,'list.shuiid'=>$vvv['id']])
                    ->find();
                if($yikouYu){
                    continue;
                }

                $vvv['shuiid'] = $vvv['id'];
                $vvv['shendt'] = date("Y-m-d", strtotime($vvv['created_at']));
                $vvv['dt'] = $iskaipiao['dt'];
                $jiqidt = getNextmonthFirstday($iskaipiao['dt']);

                //起息日：开票日期的下个月的1号，无开票日期则利息为0      结息日：到账日期,没有则取当前日期
                //税款保证金利息=周数 × 周息        周数=（计息期止-计息期起+1）/ 7，取整
                $vvv['lixifee'] = 0;
                if($iskaipiao['dt'] && $iskaipiao['dt'] != '0000-00-00' && $jiqidt < $daodt){
                    $zhoushu = ceil(  (( strtotime($daodt) - strtotime($jiqidt) )/24/60/60 + 1) / 7 );
                    $vvv['lixifee'] = round($vvv['baofee']*$zhoushu*$vvv['ratexi']/100, 2);
                }

                $vvv['kouxing'] = '未收取';
                $list[] = $vvv;
            }
        }

        $xuhaoSort = array_column($list,'xuhao');
        array_multisort($xuhaoSort,SORT_DESC, $list);
        $data['data'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_koulei_gongjie(Request $request){
        $enginid = $request->post('enginid', 0, 'int');
        $daodt = $request->post('daodt', date("Y-m-d"), 'string');
        $gofuer = $request->post('gofuer', '', 'string');

        $list = [];
        if($enginid){
            $gofuer = EnginModel::where(['id'=>$enginid])->value('gofuer');
        }


        $jieList = HouTiaoModel::alias('tiao')
            ->field('tiao.*,jie.chuer,jie.jieer,jie.number,jie.money as jiemoney')
            ->join('hou_jie jie','jie.id=tiao.jieid')
            ->where(['tiao.comid'=>$this->userinfo['comid'],'tiao.deleted_user'=>0,'tiao.isturn'=>1,'tiao.status'=>1])
            ->where(['jie.jieer'=>$gofuer])
            ->select()->toArray();

        foreach ($jieList as $item){
            if($item['daodt'] <= $daodt){
                $item = (new HouService())->get_tiaoinfo($item, $daodt);
                if($item['statename'] != '已还款'){
                    $list[] = $item;
                }
            }
        }

        $data['data'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_koulei_kuaifei(Request $request){
        $enginid = $request->post('enginid', 0, 'int');
        $daodt = $request->post('daodt', date("Y-m-d"), 'string');
        $gofuer = $request->post('gofuer', '', 'string');

        $list = [];
        if($enginid){
            $gofuer = EnginModel::where(['id'=>$enginid])->value('gofuer');
        }
        $enginList = EnginModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
            ->where(['gofuer'=>$gofuer])->select()->toArray();

        foreach ($enginList as $item){
            $jiList = KuaiJiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
                ->where(['isgua'=>'是','enginid'=>$item['id']])->select()->toArray();
            foreach ($jiList as $vvv){
                $vvv['xuhao'] = $item['id'] == $enginid ? 1 : 0;
                $vvv['enginname'] = $item['name'];
                $vvv['kuailei'] = '寄件';
                $vvv['kuaitype'] = 1;
                $vvv['kuaidt'] = $vvv['jidt'];

                $yikouHui = HouKouKuaifeiModel::alias('list')
                    ->join('hou_gonghui hou','hou.id=list.zhuid')
                    ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                    ->where(['list.type'=>1,'list.kuaitype'=>1,'list.kuaiid'=>$vvv['id']])
                    ->find();
                if($yikouHui){
                    continue;
                }
                $yikouYu = HouKouKuaifeiModel::alias('list')
                    ->join('hou_gongyu hou','hou.id=list.zhuid')
                    ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                    ->where(['list.type'=>2,'list.kuaitype'=>1,'list.kuaiid'=>$vvv['id']])
                    ->find();
                if($yikouYu){
                    continue;
                }

                $vvv['kouxing'] = '未收取';
                $list[] = $vvv;
            }

            $shouList = KuaiShouModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
                ->where(['isgua'=>'是','enginid'=>$item['id']])->select()->toArray();
            foreach ($shouList as $vvv){
                $vvv['xuhao'] = $item['id'] == $enginid ? 1 : 0;
                $vvv['enginname'] = $item['name'];
                $vvv['kuailei'] = '收件';
                $vvv['kuaitype'] = 2;
                $vvv['kuaidt'] = $vvv['jidt'];

                $yikouHui = HouKouKuaifeiModel::alias('list')
                    ->join('hou_gonghui hou','hou.id=list.zhuid')
                    ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                    ->where(['list.type'=>1,'list.kuaitype'=>2,'list.kuaiid'=>$vvv['id']])
                    ->find();
                if($yikouHui){
                    continue;
                }
                $yikouYu = HouKouKuaifeiModel::alias('list')
                    ->join('hou_gongyu hou','hou.id=list.zhuid')
                    ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                    ->where(['list.type'=>2,'list.kuaitype'=>2,'list.kuaiid'=>$vvv['id']])
                    ->find();
                if($yikouYu){
                    continue;
                }

                $vvv['kouxing'] = '未收取';
                $list[] = $vvv;
            }
        }

        $xuhaoSort = array_column($list,'xuhao');
        array_multisort($xuhaoSort,SORT_DESC, $list);
        $data['data'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }

    public function list_hou_gongyu(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['yu.deleted_user','=', 0];
        $where[] = ['yu.comid','=',$this->userinfo['comid']];
        $where[] = ['yu.isturn','=',1];
        $where[] = ['yu.status','in',[0,1]];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['yu.gofuer|yu.gofutel|yu.zhai|yu.flowuser','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['gofuer']) && !empty($param['gofuer'])){
            $where[] = ['yu.gofuer','like', '%'.$param['gofuer'].'%'];
        }
        if(isset($param['gofutel']) && !empty($param['gofutel'])){
            $where[] = ['yu.gofutel','like', '%'.$param['gofutel'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['yu.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['yu.status','=', $param['status']];
        }
        if(isset($param['incdaotype']) && !empty($param['incdaotype'])){
            $where[] = ['yu.incdaotype','=', $param['incdaotype']];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['yu.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['iskou']) && !empty($param['iskou'])){
            $where[] = ['yu.iskou','=', $param['iskou']];
        }
        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['yu.flowuser','like', '%'.$param['flowuser'].'%'];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $created_atArr = explode(' - ', $param['created_at']);
            $where[] = ['yu.created_at','between', [$created_atArr[0].' 00:00:00', $created_atArr[1].' 23:59:59']];
        }


        $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
        $data['is_incdaotype'] = isset($param['incdaotype']) ? $param['incdaotype'] : '';

        $data['data'] = HouGongyuModel::alias('yu')
            ->field('yu.*,acc.name as accname,acc.accnum')
            ->leftJoin('account acc','acc.id=yu.accid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('hou_gongyu', 'yu'))
            ->order('yu.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['incdaotype_name'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);
                $item['accid_name'] = $item['accname'].'('.$item['accnum'].')';
                $item['koulei_name'] = (new HouService())->get_hou_koulei_name($item['koulei']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('hou_gongyu');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_hou_gongyu(Request $request){
        $param = $request->post();
        $data = HouGongyuModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            HouGongyuModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'hou_gongyu', $data['id'], '工程负责人预收款');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_hou_gongyu(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = HouGongyuModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }
        $data['data']['incdaotype_name'] = (new OptService())->get_base_showname('cai_income_incdaotype', $data['data']['incdaotype']);
        $data['data']['tonger_name'] = UserModel::get_realname_by_ids($data['data']['tonger']);
        $data['account'] = AccountModel::get_info_by_pk($data['data']['accid']);

        if($data['data']['gofuer']){
            $data['data']['hougongyumoney'] = (new HouService())->get_hou_gongyu_money($data['data']['gofuer']);
            $data['data']['hougongyukoufee'] = (new HouService())->get_hou_gongyu_koufee($data['data']['gofuer']);
            $data['data']['hougongzhimoneyqugoer'] = (new HouService())->get_hou_gongzhi_money_qugoer($data['data']['gofuer']);
            $data['data']['hougongshuimoneygoquan'] = (new HouService())->get_hou_gongshui_money_goquan($data['data']['gofuer']);
            $data['data']['hougongyukeyong'] = round($data['data']['hougongyumoney']+$data['data']['hougongshuimoneygoquan']-$data['data']['hougongyukoufee']-$data['data']['hougongzhimoneyqugoer'],2);
        }


        if($data['data']['iskou'] == '是'){
            $data['data']['kouleiArr'] = explode(',', $data['data']['koulei']);
            $data['data']['koulei_name'] = (new HouService())->get_hou_koulei_name($data['data']['koulei']);

            $fuwu_list = HouKouFuwuModel::where(['type'=>2,'zhuid'=>$data['data']['id']])->select()->toArray();
            foreach ($fuwu_list as &$vvfuwu){
                $vvfuwu['houkoulei_name'] = (new OptService())->get_base_showname('cai_houkoulei', $vvfuwu['houkoulei']);
                $vvfuwu['name'] = EnginModel::where(['id'=>$vvfuwu['enginid']])->value('name');

                $vvfuwu['shimoney'] = (new EnginService())->get_contract_shi_money($vvfuwu['enginid']);
                $vvfuwu['shijiemoney'] = (new EnginService())->get_contract_shi_jie_money($vvfuwu['enginid']);

                $vvfuwu['ratefu'] = ContractNeiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
                    ->where(['enginid'=>$vvfuwu['enginid']])->order('id DESC')->value('ratefu');

                $vvfuwu['yingkoufee'] = 0;
                if($vvfuwu['shijiemoney']){
                    $vvfuwu['yingkoufee'] = round($vvfuwu['shijiemoney']*$vvfuwu['ratefu']/100, 2);
                }else{
                    $vvfuwu['yingkoufee'] = round($vvfuwu['shimoney']*$vvfuwu['ratefu']/100, 2);
                }

                $vvfuwu['yikoufee'] = HouKouFuwuModel::alias('list')
                    ->join('hou_gonghui hou','hou.id=list.zhuid')
                    ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                    ->where(['list.type'=>1,'list.enginid'=>$vvfuwu['enginid']])
                    ->sum('list.koufee');
                $vvfuwu['yikoufee'] += HouKouFuwuModel::alias('list')
                    ->join('hou_gongyu hou','hou.id=list.zhuid')
                    ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                    ->where(['list.type'=>2,'list.enginid'=>$vvfuwu['enginid']])
                    ->sum('list.koufee');
                $vvfuwu['daikoufee'] = round($vvfuwu['yingkoufee'] - $vvfuwu['yikoufee'], 2);
                $vvfuwu['daikoufee'] = $vvfuwu['daikoufee']  > 0 ? $vvfuwu['daikoufee'] : 0;
            }
            $data['fuwu_list'] = $fuwu_list;


            $shuifei_list = HouKouShuifeiModel::where(['type'=>2,'zhuid'=>$data['data']['id']])->select()->toArray();
            foreach ($shuifei_list as &$vvshuifei){
                $vvshuifei['houkoulei_name'] = (new OptService())->get_base_showname('cai_houkoulei', $vvshuifei['houkoulei']);
                $shuiinfo = EnginShuiModel::where(['id'=>$vvshuifei['shuiid']])->find();
                $piaoinfo = PiaokaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
                    ->where(['enginid'=>$shuiinfo['enginid'],'shuiid'=>$vvshuifei['shuiid'],'shuishou'=>2])->find();

                $vvshuifei['name'] = $shuiinfo['name'];
                $vvshuifei['enginname'] = EnginModel::where(['id'=>$shuiinfo['enginid']])->value('name');
                $vvshuifei['shendt'] = date("Y-m-d", strtotime($shuiinfo['created_at']));
                $vvshuifei['xiaofee'] = $shuiinfo['xiaofee'];
                $vvshuifei['dt'] = $piaoinfo['dt'];
                $vvshuifei['baofee'] = $shuiinfo['baofee'];
            }
            $data['shuifei_list'] = $shuifei_list;


            $gongjie_list = HouKouGongjieModel::where(['type'=>2,'zhuid'=>$data['data']['id']])->select()->toArray();
            foreach ($gongjie_list as &$vvgongjie){
                $vvgongjie['houkoulei_name'] = (new OptService())->get_base_showname('cai_houkoulei', $vvgongjie['houkoulei']);
                $tiaoinfo = HouTiaoModel::where(['id'=>$vvgongjie['tiaoid']])->find();
                $tiaoinfo = (new HouService())->get_tiaoinfo($tiaoinfo, $data['data']['dt']);
                $vvgongjie['number'] = $tiaoinfo['number'];
                $vvgongjie['chuer'] = $tiaoinfo['chuer'];
                $vvgongjie['money'] = $tiaoinfo['money'];
                $vvgongjie['daodt'] = $tiaoinfo['daodt'];
                $vvgongjie['yueshu_zhan'] = $tiaoinfo['yueshu_zhan'];
                $vvgongjie['daihuanbenfee'] = $tiaoinfo['daihuanbenfee'];
                $vvgongjie['daihuanlifee'] = $tiaoinfo['daihuanlifee'];
                $vvgongjie['daihuanallfee'] = $tiaoinfo['daihuanallfee'];
            }
            $data['gongjie_list'] = $gongjie_list;


            $kuaifei_list = HouKouKuaifeiModel::where(['type'=>2,'zhuid'=>$data['data']['id']])->select()->toArray();
            foreach ($kuaifei_list as &$vvkuaifei){
                $vvkuaifei['houkoulei_name'] = (new OptService())->get_base_showname('cai_houkoulei', $vvkuaifei['houkoulei']);
                if($vvkuaifei['kuaitype'] == 1){
                    $jiinfo = KuaiJiModel::where(['id'=>$vvkuaifei['kuaiid']])->find();
                    $vvkuaifei['enginname'] = EnginModel::where(['id'=>$jiinfo['enginid']])->value('name');
                    $vvkuaifei['kuailei'] = '寄件';
                    $vvkuaifei['yunnum'] = $jiinfo['yunnum'];
                    $vvkuaifei['kuaidt'] = $jiinfo['jidt'];
                    $vvkuaifei['futype'] = $jiinfo['futype'];
                    $vvkuaifei['kuaifee'] = $jiinfo['kuaifee'];
                }else{
                    $shouinfo = KuaiShouModel::where(['id'=>$vvkuaifei['kuaiid']])->find();
                    $vvkuaifei['enginname'] = EnginModel::where(['id'=>$shouinfo['enginid']])->value('name');
                    $vvkuaifei['kuailei'] = '收件';
                    $vvkuaifei['yunnum'] = $shouinfo['yunnum'];
                    $vvkuaifei['kuaidt'] = $shouinfo['jidt'];
                    $vvkuaifei['futype'] = $shouinfo['futype'];
                    $vvkuaifei['kuaifee'] = $shouinfo['kuaifee'];
                }
            }
            $data['kuaifei_list'] = $kuaifei_list;


            $qifei_list = HouKouQifeiModel::where(['type'=>2,'zhuid'=>$data['data']['id']])->select()->toArray();
            foreach ($qifei_list as &$vvqifei){
                $vvqifei['houkoulei_name'] = (new OptService())->get_base_showname('cai_houkoulei', $vvqifei['houkoulei']);
                $vvqifei['enginname'] = EnginModel::where(['id'=>$vvqifei['enginid']])->value('name');
            }
            $data['qifei_list'] = $qifei_list;
        }


        $data['files'] = (new OptService())->get_file('hou_gongyu', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }


    public function piaoming_hou_bao(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['bao.deleted_user','=', 0];
        $where[] = ['bao.comid','=',$this->userinfo['comid']];
        $where[] = ['bao.isturn','=',1];
        $where[] = ['bao.status','in',[0,1]];



        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['bill.sericnum|bao.flowuser|list.xiaoer|list.nums','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['sericnum']) && !empty($param['sericnum'])){
            $where[] = ['bill.sericnum','like', '%'.$param['sericnum'].'%'];
        }
        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['bao.flowuser','like', '%'.$param['flowuser'].'%'];
        }
        if(isset($param['kaidt']) && !empty($param['kaidt'])){
            $where[] = ['list.kaidt','between', explode(' - ', $param['kaidt'])];
        }
        if(isset($param['xiaoer']) && !empty($param['xiaoer'])){
            $where[] = ['list.xiaoer','like', '%'.$param['xiaoer'].'%'];
        }
        if(isset($param['listnums']) && !empty($param['listnums'])){
            $where[] = ['list.nums','like', '%'.$param['listnums'].'%'];
        }
        if(isset($param['qianpiaotype']) && $param['qianpiaotype'] != ''){
            $where[] = ['list.qianpiaotype','=', $param['qianpiaotype']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['bao.status','=', $param['status']];
        }

        $data['qianpiaotype'] = (new OptService())->get_base_info('cai_qianpiaotype', 2);
        $data['is_qianpiaotype'] = isset($param['qianpiaotype']) ? $param['qianpiaotype'] : '';


        $data['data'] = HouBaoPiaoModel::alias('list')
            ->field('bao.*,bill.sericnum,list.id as listid,list.xiaoer as xiaoer,list.qianpiaotype,list.nums as listnums,
            list.kaidt as listkaidt,list.fee as listfee,list.rate as listrate,list.ratefee as listratefee,list.money as listmoney,
            list.explain as listexplain,list.huidt as listhuidt')
            ->join('hou_bao bao','bao.id=list.baoid')
            ->leftJoin('flow_bill bill','bill.id=bao.billid')
            ->where($where)
            ->order('bao.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['qianpiaotype_name'] = (new OptService())->get_base_showname('cai_qianpiaotype', $item['qianpiaotype']);
                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function detail_hou_bao(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['bao.deleted_user','=', 0];
        $where[] = ['bao.comid','=',$this->userinfo['comid']];
        $where[] = ['bao.isturn','=',1];
        $where[] = ['bao.status','in',[0,1]];



        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['bill.sericnum|bao.flowuser|list.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['sericnum']) && !empty($param['sericnum'])){
            $where[] = ['bill.sericnum','like', '%'.$param['sericnum'].'%'];
        }
        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['bao.flowuser','like', '%'.$param['flowuser'].'%'];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $created_atArr = explode(' - ', $param['created_at']);
            $where[] = ['bao.created_at','between', [$created_atArr[0].' 00:00:00', $created_atArr[1].' 23:59:59']];
        }
        if(isset($param['listname']) && !empty($param['listname'])){
            $where[] = ['list.name','like', '%'.$param['listname'].'%'];
        }
        if(isset($param['baokuanlei']) && $param['baokuanlei'] != ''){
            $where[] = ['list.baokuanlei','=', $param['baokuanlei']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['bao.status','=', $param['status']];
        }

        $data['baokuanlei'] = (new OptService())->get_base_info('cai_baokuanlei', 2);
        $data['is_baokuanlei'] = isset($param['baokuanlei']) ? $param['baokuanlei'] : '';

        $data['data'] = HouBaoListModel::alias('list')
            ->field('bao.*,bill.sericnum,list.id as listid,list.name as listname,list.baokuanlei,list.money as listmoney,
            list.explain as listexplain,list.dt as listdt')
            ->join('hou_bao bao','bao.id=list.baoid')
            ->leftJoin('flow_bill bill','bill.id=bao.billid')
            ->where($where)
            ->order('bao.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['baokuanlei_name'] = (new OptService())->get_base_showname('cai_baokuanlei', $item['baokuanlei']);
                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_hou_bao(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['bao.deleted_user','=', 0];
        $where[] = ['bao.comid','=',$this->userinfo['comid']];
        $where[] = ['bao.isturn','=',1];
        $where[] = ['bao.status','in',[0,1]];


        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['bao.enginid','=', $param['enginid']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|bao.zhai|bao.fuer','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['bao.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['fuer']) && !empty($param['fuer'])){
            $where[] = ['bao.fuer','like', '%'.$param['fuer'].'%'];
        }
        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['bao.flowuser','like', '%'.$param['flowuser'].'%'];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $created_atArr = explode(' - ', $param['created_at']);
            $where[] = ['bao.created_at','between', [$created_atArr[0].' 00:00:00', $created_atArr[1].' 23:59:59']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['bao.status','=', $param['status']];
        }


        $data['data'] = HouBaoModel::alias('bao')
            ->field('bao.*,e.name as enginname')
            ->leftJoin('engin e','e.id=bao.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('hou_bao', 'bao'))
            ->order('bao.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['incdaotype_name'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);

                $wangPiaofee = HouBaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['isturn'=>1,'created_user'=>$item['created_user']])->where('status','in',[0,1])->sum('piaofee');
                $wangMoney = HouBaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['isturn'=>1,'created_user'=>$item['created_user']])->where('status','in',[0,1])->sum('money');

                $item['piaosheng'] = round($wangPiaofee - $wangMoney, 2);

                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('hou_bao');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_hou_bao(Request $request){
        $param = $request->post();
        $data = HouBaoModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            HouBaoModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'hou_bao', $data['id'], '费用报销');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_hou_bao(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = HouBaoModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
        $data['data']['incdaotype_name'] = (new OptService())->get_base_showname('cai_income_incdaotype', $data['data']['incdaotype']);

        $list = HouBaoListModel::where(['baoid'=>$data['data']['id']])->select()->toArray();
        foreach ($list as &$item){
            $item['baokuanlei_name'] = (new OptService())->get_base_showname('cai_baokuanlei', $item['baokuanlei']);
        }
        $data['list'] = $list;


        $list_piao = HouBaoPiaoModel::where(['baoid'=>$data['data']['id']])->select()->toArray();
        foreach ($list_piao as &$item){
            $item['qianpiaotype_name'] = (new OptService())->get_base_showname('cai_qianpiaotype', $item['qianpiaotype']);
        }
        $data['list_piao'] = $list_piao;


        $wangPiaofee = HouBaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['isturn'=>1,'created_user'=>$data['data']['created_user']])->where('status','in',[0,1])->sum('piaofee');
        $wangMoney = HouBaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['isturn'=>1,'created_user'=>$data['data']['created_user']])->where('status','in',[0,1])->sum('money');

        $data['data']['piaosheng'] = round($wangPiaofee - $wangMoney);


        $data['files'] = (new OptService())->get_file('hou_bao', $data['data']['id']);
        $data['kuaninfo'] = (new FinanceService())->get_acckuan_info_bybillid($data['data']['billid']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }
    public function piao_hou_bao(Request $request){
        if($request->isPost()){
            $param = $request->post();

            $files = array_filter($param['files']);
            unset($param['files']);
            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入发票明细');
            }

            Db::startTrans();
            try {

                HouBaoPiaoModel::where(['baoid'=>$param['id']])->delete();
                if(count($list) > 0){
                    HouBaoModel::where(['id'=>$param['id']])->update(['ispiao'=>'有','piaofee'=>$param['piaofee']]);
                    foreach ($list as $item) {
                        $item['baoid'] = $param['id'];
                        HouBaoPiaoModel::insert($item);
                    }
                }
                (new OptService())->relation_file($files,'hou_bao',$param['id']);

                (new OptService())->add_flow_log('补票', 'hou_bao', $param['id']);
                (new OptService())->add_log_list('补票', 'hou_bao', $param['id'], '费用报销');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = HouBaoModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }
            $list = HouBaoPiaoModel::where(['baoid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;
            $data['qianpiaotype'] = (new OptService())->get_base_info('cai_qianpiaotype');
            $data['files'] = (new OptService())->get_file('hou_bao', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }



    public function hou_chi_gonghui(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.isturn','=',1];
        $where[] = ['e.status','=',1];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['e.enginid','=', $param['enginid']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|e.gofuer','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['gofuer']) && !empty($param['gofuer'])){
            $where[] = ['e.gofuer','like', '%'.$param['gofuer'].'%'];
        }

        $data['data'] = EnginModel::alias('e')
            ->field('e.*,gui_unit.name as gui_unit_name')
            ->leftJoin('dept gui_unit','gui_unit.id=e.gui_unit')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin', 'e'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['state_name'] = (new EnginService())->get_engin_state_name($item);

                $item['shimoney'] = 0;
                $item['shiid'] = 0;
                $item['shiinfo'] = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])->where(['enginid'=>$item['id']])->find();
                if($item['shiinfo']){
                    $item['shimoney'] = $item['shiinfo']['money'];
                    $item['shiid'] = $item['shiinfo']['id'];
                }


                $item['shijiemoney'] = (new EnginService())->get_contract_shi_jie_money($item['id']);
                $item['hougonghuimoney'] = (new HouService())->get_hou_gonghui_money($item['id']);
                $item['hougongzhimoneyquben'] = (new HouService())->get_hou_gongzhi_money_quben($item['id']);
                $item['hougonghuikoufee'] = (new HouService())->get_hou_gonghui_koufee($item['id']);
                $item['qizankoufee'] = HouKouQifeiModel::alias('list')
                    ->join('hou_gonghui hou','hou.id=list.zhuid')
                    ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                    ->where(['list.type'=>1, 'list.enginid'=>$item['id'],'kouxing'=>'暂扣'])
                    ->sum('list.koufee');


                $item['jiedongkoufee'] = HouKouQifeiModel::alias('list')
                    ->join('hou_gonghui hou','hou.id=list.zhuid')
                    ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                    ->where(['list.type'=>1, 'list.enginid'=>$item['id'],'isdong'=>1])
                    ->sum('list.koufee');
//                $item['jiedongkoufee'] += HouKouQifeiModel::alias('list')
//                    ->join('hou_gongyu hou','hou.id=list.zhuid')
//                    ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
//                    ->where(['list.type'=>2, 'list.enginid'=>$item['id'],'isdong'=>1])
//                    ->sum('list.koufee');

                $item['yuliufee'] = round($item['qizankoufee'] - $item['jiedongkoufee'], 2);
                $item['hougonghuikeyong'] = round($item['hougonghuimoney'] - $item['hougongzhimoneyquben'] - $item['hougonghuikoufee'] + $item['jiedongkoufee'], 2);

                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function hou_chi_gongyu(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.isturn','=',1];
        $where[] = ['e.status','=',1];
        $where[] = ['e.gofuer','neq',''];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.gofuer','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['gofuer']) && !empty($param['gofuer'])){
            $where[] = ['e.gofuer','like', '%'.$param['gofuer'].'%'];
        }


        $data['data'] = EnginModel::alias('e')
            ->field('e.gofuer')
            ->where($where)
            ->group('e.gofuer')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['hougongyumoney'] = (new HouService())->get_hou_gongyu_money($item['gofuer']);
                $item['hougongyukoufee'] = (new HouService())->get_hou_gongyu_koufee($item['gofuer']);
                $item['hougongzhimoneyqugoer'] = (new HouService())->get_hou_gongzhi_money_qugoer($item['gofuer']);

                $item['qizankoufee'] = HouKouQifeiModel::alias('list')
                    ->join('hou_gongyu hou','hou.id=list.zhuid')
                    ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                    ->where(['list.type'=>2, 'hou.gofuer'=>$item['gofuer'],'kouxing'=>'暂扣'])
                    ->sum('list.koufee');


                $item['jiedongkoufee'] = HouKouQifeiModel::alias('list')
                    ->join('hou_gongyu hou','hou.id=list.zhuid')
                    ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                    ->where(['list.type'=>2, 'hou.gofuer'=>$item['gofuer'],'isdong'=>1])
                    ->sum('list.koufee');


                $item['yuliufee'] = round($item['qizankoufee'] - $item['jiedongkoufee'], 2);

                $item['hougongshuimoneygoquan'] = (new HouService())->get_hou_gongshui_money_goquan($item['gofuer']);
                $item['hougongyukeyong'] = round($item['hougongyumoney']-$item['hougongyukoufee']-$item['hougongzhimoneyqugoer']+$item['jiedongkoufee']+$item['hougongshuimoneygoquan'],2);

                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function hou_chi_gongyu2222(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['yu.deleted_user','=', 0];
        $where[] = ['yu.comid','=',$this->userinfo['comid']];
        $where[] = ['yu.isturn','=',1];
        $where[] = ['yu.status','=',1];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['yu.gofuer','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['gofuer']) && !empty($param['gofuer'])){
            $where[] = ['yu.gofuer','like', '%'.$param['gofuer'].'%'];
        }


        $data['data'] = HouGongyuModel::alias('yu')
            ->field('yu.gofuer')
            ->where($where)
            ->where((new MenuService())->get_miss_where('hou_gongyu', 'yu'))
            ->order('yu.id DESC')
            ->group('yu.gofuer')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){

                $item['hougongyumoney'] = (new HouService())->get_hou_gongyu_money($item['gofuer']);
                $item['hougongyukoufee'] = (new HouService())->get_hou_gongyu_koufee($item['gofuer']);
                $item['hougongzhimoneyqugoer'] = (new HouService())->get_hou_gongzhi_money_qugoer($item['gofuer']);

                $item['jiedongkoufee'] = HouKouQifeiModel::alias('list')
                    ->join('hou_gonghui hou','hou.id=list.zhuid')
                    ->join('engin e','e.id=list.enginid')
                    ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                    ->where(['list.type'=>1, 'e.gofuer'=>$item['gofuer'],'isdong'=>1])
                    ->sum('list.koufee');

                $item['jiedongkoufee'] += HouKouQifeiModel::alias('list')
                    ->join('hou_gongyu hou','hou.id=list.zhuid')
                    ->join('engin e','e.id=list.enginid')
                    ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                    ->where(['list.type'=>2, 'e.gofuer'=>$item['gofuer'],'isdong'=>1])
                    ->sum('list.koufee');

                $item['hougongshuimoneygoquan'] = (new HouService())->get_hou_gongshui_money_goquan($item['gofuer']);
                $item['hougongyukeyong'] = round($item['hougongyumoney']-$item['hougongyukoufee']-$item['hougongzhimoneyqugoer']+$item['jiedongkoufee']+$item['hougongshuimoneygoquan'],2);

                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function hou_chi_gongzhi(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['zhi.deleted_user','=', 0];
        $where[] = ['zhi.comid','=',$this->userinfo['comid']];
        $where[] = ['zhi.isturn','=',1];
        $where[] = ['zhi.status','in',[0,1]];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['zhi.enginid','=', $param['enginid']];
        }

        if(isset($param['dealtype']) && !empty($param['dealtype'])){
            $where[] = ['deal.type','=', $param['dealtype']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|list.fuer|e.gofuer','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['gofuer']) && !empty($param['gofuer'])){
            $where[] = ['e.gofuer','like', '%'.$param['gofuer'].'%'];
        }
        if(isset($param['fuer']) && !empty($param['fuer'])){
            $where[] = ['list.fuer','like', '%'.$param['fuer'].'%'];
        }
        if(isset($param['zhikuanlei']) && !empty($param['zhikuanlei'])){
            $where[] = ['list.zhikuanlei','=', $param['zhikuanlei']];
        }
        if(isset($param['qutype']) && !empty($param['qutype'])){
            $where[] = ['qu.type','=', $param['qutype']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['zhi.status','=', $param['status']];
        }
        if(isset($param['gui_unit']) && !empty($param['gui_unit'])){
            $where[] = ['gui_unit.id','=', $param['gui_unit']];
        }

        $data['zhikuanlei'] = (new OptService())->get_base_info('cai_zhikuanlei');
        $data['is_zhikuanlei'] = isset($param['zhikuanlei']) ? $param['zhikuanlei'] : '';


        $whereQu = '';
        if(isset($param['qu_enginid']) && !empty($param['qu_enginid'])){
            $whereQu = "(qu.type=2 and zhi.enginid=".$param['qu_enginid'].")";
            $whereQu .= " or (qu.type=3 and qu.qienid=".$param['qu_enginid'].")";
        }

        $whereQien = '';
        if(isset($param['qu_enginname']) && !empty($param['qu_enginname'])){
            $qienidArr = EnginModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1])
                ->where("name like '%".$param['qu_enginname']."%'")->column('id');
            if(!$qienidArr){
                $qienidArr = [0];
            }
            $qienidstr = "(".implode(',', $qienidArr).")";
            $whereQu = "(qu.type=2 and zhi.enginid in ".$qienidstr.")";
            $whereQu .= " or (qu.type=3 and qu.qienid in ".$qienidstr.")";
        }

        $data['gui_unit'] = (new UserService())->get_chaninfo_all();
        $data['is_gui_unit'] = isset($param['gui_unit']) ? $param['gui_unit'] : '';

        $data['data'] = HouGongzhiQuModel::alias('qu')
            ->field('qu.*,zhi.billid,zhi.status,list.zhikuanlei,list.fuer,list.money as listmoney,list.explain as listexplain
            ,e.name as enginname,e.gofuer,gui_unit.name as gui_unit_name')
            ->join('hou_gongzhi_list list','list.id=qu.gongzhilistid')
            ->join('hou_gongzhi zhi','zhi.id=qu.gongzhiid')
            ->join('engin e','e.id=zhi.enginid')
            ->leftJoin('dealcom deal','deal.name=list.fuer')
            ->leftJoin('dept gui_unit','gui_unit.id=e.gui_unit')
            ->where($where)
            ->where($whereQu)
            ->where($whereQien)
            ->where((new MenuService())->get_miss_where('hou_gongzhi', 'zhi'))
            ->order('zhi.id DESC')
            ->group('list.id')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['shulai'] = '工程付款';
                $item['sericnum'] = FlowBillModel::where(['comid'=>$this->userinfo['comid'],'id'=>$item['billid']])->value('sericnum');
                $item['zhikuanlei_name'] = (new OptService())->get_base_showname('cai_zhikuanlei', $item['zhikuanlei']);

                if($item['type'] == 1){
                    $item['type_name'] = '工程负责人预收款';
                }elseif($item['type'] == 2){
                    $item['type_name'] = $item['enginname'];
                }elseif($item['type'] == 3){
                    $item['type_name'] = EnginModel::where(['id'=>$item['qienid']])->value('name');
                }
                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function hou_chi_kou_fuwu(Request $request){
        $param = $request->post();
        get_page($param);

        $where = [];
        $where[] = ['hui.status|yu.status','in', [0,1]];
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['list.enginid','=', $param['enginid']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|e.gofuer','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['hui.status|yu.status','=',$param['status']];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['hui.dt|yu.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['gofuer']) && !empty($param['gofuer'])){
            $where[] = ['e.gofuer','like', '%'.$param['gofuer'].'%'];
        }
        if(isset($param['gui_unit']) && !empty($param['gui_unit'])){
            $where[] = ['e.gui_unit','=', $param['gui_unit']];
        }



        $data['gui_unit'] = (new UserService())->get_chaninfo_all();
        $data['is_gui_unit'] = isset($param['gui_unit']) ? $param['gui_unit'] : '';



        $data['data'] = HouKouFuwuModel::alias('list')
            ->field('list.*,e.name as enginname,e.gofuer,gui_unit.name as gui_unit_name
            ,hui.billid as billid_hui,hui.status as status_hui,hui.koufee as koufee_hui,hui.enginid as enginid_hui,hui.dt as dt_hui
            ,yu.billid as billid_yu,yu.status as status_yu,yu.koufee as koufee_yu,yu.gofuer as gofuer_yu,yu.dt as dt_yu')
            ->leftJoin('hou_gonghui hui','hui.id=list.zhuid and list.type=1')
            ->leftJoin('hou_gongyu yu','yu.id=list.zhuid and list.type=2')
            ->join('engin e','e.id=list.enginid')
            ->leftJoin('dept gui_unit','gui_unit.id=e.gui_unit')
            ->where("hui.deleted_user = 0 or yu.deleted_user = 0")
            ->where("hui.comid = ".$this->userinfo['comid']." or yu.comid = ".$this->userinfo['comid'])
            ->where("hui.isturn = 1 or yu.isturn = 1")
            ->where($where)
            ->order('list.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                if($item['type'] == 1){
                    $item['shulai'] = '工程回款';
                    $item['billid'] = $item['billid_hui'];
                    $item['status'] = $item['status_hui'];
                    $item['dt'] = $item['dt_hui'];
                    $item['qu_name'] = EnginModel::where(['id'=>$item['enginid_hui']])->value('name');
                }else{
                    $item['shulai'] = '工程负责人预收款';
                    $item['billid'] = $item['billid_yu'];
                    $item['status'] = $item['status_yu'];
                    $item['dt'] = $item['dt_yu'];
                    $item['qu_name'] = $item['gofuer_yu'];
                }
                $item['houkoulei_name'] = (new OptService())->get_base_showname('cai_houkoulei', $item['houkoulei']);
                $item['sericnum'] = FlowBillModel::where(['comid'=>$this->userinfo['comid'],'id'=>$item['billid']])->value('sericnum');

                return $item;
            })->toArray();

//        foreach ($info as &$item){
//
//
//            $res = true;
//            if(isset($param['shulai']) && !empty($param['shulai'])){
//                if($param['shulai'] != $item['type']){
//                    $res = false;
//                }
//            }
//            if(isset($param['sericnum']) && !empty($param['sericnum'])){
//                if(strpos($item['sericnum'], $param['sericnum']) === false){
//                    $res = false;
//                }
//            }
//
//            if($res){
//                $list[] = $item;
//            }
//        }
//
//        $data['data'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function hou_chi_kou_shuifei(Request $request){
        $param = $request->post();
        get_page($param);

        $where = [];
        $where[] = ['hui.status|yu.status','in', [0,1]];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['shui.enginid','=', $param['enginid']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|e.gofuer','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['hui.status|yu.status','=',$param['status']];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['hui.dt|yu.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['gofuer']) && !empty($param['gofuer'])){
            $where[] = ['e.gofuer','like', '%'.$param['gofuer'].'%'];
        }
        if(isset($param['gui_unit']) && !empty($param['gui_unit'])){
            $where[] = ['e.gui_unit','=', $param['gui_unit']];
        }
        if(isset($param['houkoulei']) && !empty($param['houkoulei'])){
            $where[] = ['list.houkoulei','=', $param['houkoulei']];
        }

        $data['gui_unit'] = (new UserService())->get_chaninfo_all();
        $data['is_gui_unit'] = isset($param['gui_unit']) ? $param['gui_unit'] : '';


        $houkoulei = (new OptService())->get_base_info('cai_houkoulei');
        foreach ($houkoulei as $kkk=>$fff){
            if(!in_array($fff['name'], ['税费保证金','税费利息'])){
                unset($houkoulei[$kkk]);
            }
        }
        sort($houkoulei);
        $data['houkoulei'] = $houkoulei;
        $data['is_houkoulei'] = isset($param['houkoulei']) ? $param['houkoulei'] : '';

        $list = [];
        $data['data'] = HouKouShuifeiModel::alias('list')
            ->field('list.*,e.name as enginname,e.gofuer,gui_unit.name as gui_unit_name
            ,hui.billid as billid_hui,hui.status as status_hui,hui.koufee as koufee_hui,hui.enginid as enginid_hui,hui.dt as dt_hui
            ,yu.billid as billid_yu,yu.status as status_yu,yu.koufee as koufee_yu,yu.gofuer as gofuer_yu,yu.dt as dt_yu')
            ->leftJoin('hou_gonghui hui','hui.id=list.zhuid and list.type=1')
            ->leftJoin('hou_gongyu yu','yu.id=list.zhuid and list.type=2')
            ->join('engin_shui shui','shui.id=list.shuiid')
            ->join('engin e','e.id=shui.enginid')
            ->leftJoin('dept gui_unit','gui_unit.id=e.gui_unit')
            ->where("hui.deleted_user = 0 or yu.deleted_user = 0")
            ->where("hui.comid = ".$this->userinfo['comid']." or yu.comid = ".$this->userinfo['comid'])
            ->where("hui.isturn = 1 or yu.isturn = 1")
            ->where($where)
            ->order('list.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                if($item['type'] == 1){
                    $item['shulai'] = '工程回款';
                    $item['billid'] = $item['billid_hui'];
                    $item['status'] = $item['status_hui'];
                    $item['qu_name'] = EnginModel::where(['id'=>$item['enginid_hui']])->value('name');
                    $item['dt'] = $item['dt_hui'];
                }else{
                    $item['shulai'] = '工程负责人预收款';
                    $item['billid'] = $item['billid_yu'];
                    $item['status'] = $item['status_yu'];
                    $item['qu_name'] = $item['gofuer_yu'];
                    $item['dt'] = $item['dt_yu'];
                }
                $item['houkoulei_name'] = (new OptService())->get_base_showname('cai_houkoulei', $item['houkoulei']);
                $item['sericnum'] = FlowBillModel::where(['comid'=>$this->userinfo['comid'],'id'=>$item['billid']])->value('sericnum');

                return $item;
            })->toArray();

//        foreach ($info as &$item){
//            if($item['type'] == 1){
//                $item['shulai'] = '工程回款';
//                $item['billid'] = $item['billid_hui'];
//                $item['status'] = $item['status_hui'];
//                $item['qu_name'] = EnginModel::where(['id'=>$item['enginid_hui']])->value('name');
//                $item['dt'] = $item['dt_hui'];
//            }else{
//                $item['shulai'] = '工程负责人预收款';
//                $item['billid'] = $item['billid_yu'];
//                $item['status'] = $item['status_yu'];
//                $item['qu_name'] = $item['gofuer_yu'];
//                $item['dt'] = $item['dt_yu'];
//            }
//            $item['houkoulei_name'] = (new OptService())->get_base_showname('cai_houkoulei', $item['houkoulei']);
//            $item['sericnum'] = FlowBillModel::where(['comid'=>$this->userinfo['comid'],'id'=>$item['billid']])->value('sericnum');
//
//            $res = true;
//            if(isset($param['shulai']) && !empty($param['shulai'])){
//                if($param['shulai'] != $item['type']){
//                    $res = false;
//                }
//            }
//            if(isset($param['sericnum']) && !empty($param['sericnum'])){
//                if(strpos($item['sericnum'], $param['sericnum']) === false){
//                    $res = false;
//                }
//            }
//
//            if($res){
//                $list[] = $item;
//            }
//        }
//
//
//        $data['data'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function hou_chi_kou_gongjie(Request $request){
        $param = $request->post();
        get_page($param);

        $where = [];
        $where[] = ['hui.status|yu.status','in', [0,1]];
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['hui.status|yu.status','=',$param['status']];
        }
        if(isset($param['jie_number']) && !empty($param['jie_number'])){
            $where[] = ['jie.number','like', '%'.$param['jie_number'].'%'];
        }
        if(isset($param['daodt']) && !empty($param['daodt'])){
            $where[] = ['jie.daodt','between', explode(' - ', $param['daodt'])];
        }

        if(isset($param['jieid']) && !empty($param['jieid'])){
            $where[] = ['jie.id','=', $param['jieid']];
        }
        if(isset($param['tiaoid']) && !empty($param['tiaoid'])){
            $where[] = ['list.tiaoid','=', $param['tiaoid']];
        }


        $list = [];
        $data['data'] = HouKouGongjieModel::alias('list')
            ->field('list.*
            ,hui.billid as billid_hui,hui.status as status_hui,hui.koufee as koufee_hui,hui.enginid as enginid_hui,hui.dt as dt_hui
            ,yu.billid as billid_yu,yu.status as status_yu,yu.koufee as koufee_yu,yu.gofuer as gofuer_yu,yu.dt as dt_yu
            ,jie.number as jie_number,tiao.money as daomoney,tiao.daodt')
            ->leftJoin('hou_gonghui hui','hui.id=list.zhuid and list.type=1')
            ->leftJoin('hou_gongyu yu','yu.id=list.zhuid and list.type=2')
            ->leftJoin('hou_tiao tiao','tiao.id=list.tiaoid')
            ->leftJoin('hou_jie jie','jie.id=tiao.jieid')
            ->where("hui.deleted_user = 0 or yu.deleted_user = 0")
            ->where("hui.comid = ".$this->userinfo['comid']." or yu.comid = ".$this->userinfo['comid'])
            ->where("hui.isturn = 1 or yu.isturn = 1")
            ->where($where)
            ->order('list.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                if($item['type'] == 1){
                    $item['shulai'] = '工程回款';
                    $item['billid'] = $item['billid_hui'];
                    $item['status'] = $item['status_hui'];
                    $item['qu_name'] = EnginModel::where(['id'=>$item['enginid_hui']])->value('name');
                    $item['dt'] = $item['dt_hui'];
                }else{
                    $item['shulai'] = '工程负责人预收款';
                    $item['billid'] = $item['billid_yu'];
                    $item['status'] = $item['status_yu'];
                    $item['qu_name'] = $item['gofuer_yu'];
                    $item['dt'] = $item['dt_yu'];
                }
                $item['houkoulei_name'] = (new OptService())->get_base_showname('cai_houkoulei', $item['houkoulei']);
                $item['sericnum'] = FlowBillModel::where(['comid'=>$this->userinfo['comid'],'id'=>$item['billid']])->value('sericnum');
                return $item;
            })->toArray();

//        foreach ($info as &$item){
//            if($item['type'] == 1){
//                $item['shulai'] = '工程回款';
//                $item['billid'] = $item['billid_hui'];
//                $item['status'] = $item['status_hui'];
//                $item['qu_name'] = EnginModel::where(['id'=>$item['enginid_hui']])->value('name');
//                $item['dt'] = $item['dt_hui'];
//            }else{
//                $item['shulai'] = '工程负责人预收款';
//                $item['billid'] = $item['billid_yu'];
//                $item['status'] = $item['status_yu'];
//                $item['qu_name'] = $item['gofuer_yu'];
//                $item['dt'] = $item['dt_yu'];
//            }
//            $item['houkoulei_name'] = (new OptService())->get_base_showname('cai_houkoulei', $item['houkoulei']);
//            $item['sericnum'] = FlowBillModel::where(['comid'=>$this->userinfo['comid'],'id'=>$item['billid']])->value('sericnum');
//
//
//            $res = true;
//            if(isset($param['shulai']) && !empty($param['shulai'])){
//                if($param['shulai'] != $item['type']){
//                    $res = false;
//                }
//            }
//            if(isset($param['sericnum']) && !empty($param['sericnum'])){
//                if(strpos($item['sericnum'], $param['sericnum']) === false){
//                    $res = false;
//                }
//            }
//
//            if($res){
//                $list[] = $item;
//            }
//        }
//
//        $data['data'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function hou_chi_kou_kuaifei(Request $request){
        $param = $request->post();
        get_page($param);

        $where = [];
        $where[] = ['hui.status|yu.status','in', [0,1]];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['list.enginid','=', $param['enginid']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|e.gofuer','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['hui.status|yu.status','=',$param['status']];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['hui.dt|yu.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['gofuer']) && !empty($param['gofuer'])){
            $where[] = ['e.gofuer','like', '%'.$param['gofuer'].'%'];
        }
        if(isset($param['gui_unit']) && !empty($param['gui_unit'])){
            $where[] = ['e.gui_unit','=', $param['gui_unit']];
        }

        $data['gui_unit'] = (new UserService())->get_chaninfo_all();
        $data['is_gui_unit'] = isset($param['gui_unit']) ? $param['gui_unit'] : '';

        $list = [];
        $data['data'] = HouKouKuaifeiModel::alias('list')
            ->field('list.*,e.name as enginname,e.gofuer,gui_unit.name as gui_unit_name
            ,hui.billid as billid_hui,hui.status as status_hui,hui.koufee as koufee_hui,hui.enginid as enginid_hui,hui.dt as dt_hui
            ,yu.billid as billid_yu,yu.status as status_yu,yu.koufee as koufee_yu,yu.gofuer as gofuer_yu,yu.dt as dt_yu')
            ->leftJoin('hou_gonghui hui','hui.id=list.zhuid and list.type=1')
            ->leftJoin('hou_gongyu yu','yu.id=list.zhuid and list.type=2')
            ->leftJoin('engin e','e.id=list.enginid')
            ->leftJoin('dept gui_unit','gui_unit.id=e.gui_unit')
            ->where("hui.deleted_user = 0 or yu.deleted_user = 0")
            ->where("hui.comid = ".$this->userinfo['comid']." or yu.comid = ".$this->userinfo['comid'])
            ->where("hui.isturn = 1 or yu.isturn = 1")
            ->where($where)
            ->order('list.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                if($item['type'] == 1){
                    $item['shulai'] = '工程回款';
                    $item['billid'] = $item['billid_hui'];
                    $item['status'] = $item['status_hui'];
                    $item['qu_name'] = EnginModel::where(['id'=>$item['enginid_hui']])->value('name');
                    $item['dt'] = $item['dt_hui'];
                }else{
                    $item['shulai'] = '工程负责人预收款';
                    $item['billid'] = $item['billid_yu'];
                    $item['status'] = $item['status_yu'];
                    $item['qu_name'] = $item['gofuer_yu'];
                    $item['dt'] = $item['dt_yu'];
                }
                $item['houkoulei_name'] = (new OptService())->get_base_showname('cai_houkoulei', $item['houkoulei']);
                $item['sericnum'] = FlowBillModel::where(['comid'=>$this->userinfo['comid'],'id'=>$item['billid']])->value('sericnum');
                return $item;
            })->toArray();


//        foreach ($info as &$item){
//            if($item['type'] == 1){
//                $item['shulai'] = '工程回款';
//                $item['billid'] = $item['billid_hui'];
//                $item['status'] = $item['status_hui'];
//                $item['qu_name'] = EnginModel::where(['id'=>$item['enginid_hui']])->value('name');
//                $item['dt'] = $item['dt_hui'];
//            }else{
//                $item['shulai'] = '工程负责人预收款';
//                $item['billid'] = $item['billid_yu'];
//                $item['status'] = $item['status_yu'];
//                $item['qu_name'] = $item['gofuer_yu'];
//                $item['dt'] = $item['dt_yu'];
//            }
//            $item['houkoulei_name'] = (new OptService())->get_base_showname('cai_houkoulei', $item['houkoulei']);
//            $item['sericnum'] = FlowBillModel::where(['comid'=>$this->userinfo['comid'],'id'=>$item['billid']])->value('sericnum');
//
//
//            $res = true;
//            if(isset($param['shulai']) && !empty($param['shulai'])){
//                if($param['shulai'] != $item['type']){
//                    $res = false;
//                }
//            }
//            if(isset($param['sericnum']) && !empty($param['sericnum'])){
//                if(strpos($item['sericnum'], $param['sericnum']) === false){
//                    $res = false;
//                }
//            }
//
//            if($res){
//                $list[] = $item;
//            }
//        }
//        $data['data'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function hou_chi_kou_qifei(Request $request){
        $param = $request->post();
        get_page($param);

        $where = [];
        $where[] = ['hui.status|yu.status','in', [0,1]];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['list.enginid','=', $param['enginid']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|e.gofuer','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['hui.status|yu.status','=',$param['status']];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['hui.dt|yu.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['gofuer']) && !empty($param['gofuer'])){
            $where[] = ['e.gofuer','like', '%'.$param['gofuer'].'%'];
        }
        if(isset($param['gui_unit']) && !empty($param['gui_unit'])){
            $where[] = ['e.gui_unit','=', $param['gui_unit']];
        }
        if(isset($param['houkoulei']) && !empty($param['houkoulei'])){
            $where[] = ['list.houkoulei','=', $param['houkoulei']];
        }

        $data['gui_unit'] = (new UserService())->get_chaninfo_all();
        $data['is_gui_unit'] = isset($param['gui_unit']) ? $param['gui_unit'] : '';


        if(isset($param['isdong']) && $param['isdong'] != ''){
            $where[] = ['list.isdong','=', $param['isdong']];
        }
        if(isset($param['kouxing']) && !empty($param['kouxing'])){
            $where[] = ['list.kouxing','=', $param['kouxing']];
        }
        if(isset($param['listtype']) && !empty($param['listtype'])){
            $where[] = ['list.type','=', $param['listtype']];
        }

        $houkoulei = (new OptService())->get_base_info('cai_houkoulei');
        foreach ($houkoulei as $kkk=>$fff){
            if(in_array($fff['name'], ['劳务分包服务费','快递费','借款'])){
                unset($houkoulei[$kkk]);
            }
        }
        sort($houkoulei);
        $data['houkoulei'] = $houkoulei;
        $data['is_houkoulei'] = isset($param['houkoulei']) ? $param['houkoulei'] : '';

        $data['gui_unit'] = (new UserService())->get_chaninfo_all();
        $data['is_gui_unit'] = isset($param['gui_unit']) ? $param['gui_unit'] : '';


        if(isset($param['sou_houkou']) && $param['sou_houkou'] != ''){
            if($param['sou_houkou'] == 'two'){
                $where[] = ['list.houkoulei','eq', 2];
            }
            if($param['sou_houkou'] == 'notwo'){
                $where[] = ['list.houkoulei','neq', 2];
            }
        }




        $list = [];
        $data['data'] = HouKouQifeiModel::alias('list')
            ->field('list.*,e.name as enginname,e.gofuer,gui_unit.name as gui_unit_name
            ,hui.billid as billid_hui,hui.status as status_hui,hui.koufee as koufee_hui,hui.enginid as enginid_hui,hui.dt as dt_hui
            ,yu.billid as billid_yu,yu.status as status_yu,yu.koufee as koufee_yu,yu.gofuer as gofuer_yu,yu.dt as dt_yu')
            ->leftJoin('hou_gonghui hui','hui.id=list.zhuid and list.type=1')
            ->leftJoin('hou_gongyu yu','yu.id=list.zhuid and list.type=2')
            ->leftJoin('engin e','e.id=list.enginid')
            ->leftJoin('dept gui_unit','gui_unit.id=e.gui_unit')
            ->where("hui.deleted_user = 0 or yu.deleted_user = 0")
            ->where("hui.comid = ".$this->userinfo['comid']." or yu.comid = ".$this->userinfo['comid'])
            ->where("hui.isturn = 1 or yu.isturn = 1")
            ->where($where)
            ->order('list.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                if($item['type'] == 1){
                    $item['shulai'] = '工程回款';
                    $item['billid'] = $item['billid_hui'];
                    $item['status'] = $item['status_hui'];
                    $item['qu_name'] = EnginModel::where(['id'=>$item['enginid_hui']])->value('name');
                    $item['dt'] = $item['dt_hui'];
                }else{
                    $item['shulai'] = '工程负责人预收款';
                    $item['billid'] = $item['billid_yu'];
                    $item['status'] = $item['status_yu'];
                    $item['qu_name'] = $item['gofuer_yu'];
                    $item['dt'] = $item['dt_yu'];
                }
                $item['houkoulei_name'] = (new OptService())->get_base_showname('cai_houkoulei', $item['houkoulei']);
                $item['sericnum'] = FlowBillModel::where(['comid'=>$this->userinfo['comid'],'id'=>$item['billid']])->value('sericnum');

                $item['isdong_name'] = $item['isdong'] > 0 ? '解冻' : '-';
                return $item;
            })->toArray();

//        foreach ($info as &$item){
//            if($item['type'] == 1){
//                $item['shulai'] = '工程回款';
//                $item['billid'] = $item['billid_hui'];
//                $item['status'] = $item['status_hui'];
//                $item['qu_name'] = EnginModel::where(['id'=>$item['enginid_hui']])->value('name');
//                $item['dt'] = $item['dt_hui'];
//            }else{
//                $item['shulai'] = '工程负责人预收款';
//                $item['billid'] = $item['billid_yu'];
//                $item['status'] = $item['status_yu'];
//                $item['qu_name'] = $item['gofuer_yu'];
//                $item['dt'] = $item['dt_yu'];
//            }
//            $item['houkoulei_name'] = (new OptService())->get_base_showname('cai_houkoulei', $item['houkoulei']);
//            $item['sericnum'] = FlowBillModel::where(['comid'=>$this->userinfo['comid'],'id'=>$item['billid']])->value('sericnum');
//
//            $item['isdong_name'] = $item['isdong'] > 0 ? '解冻' : '-';
//
//
//            $res = true;
//            if(isset($param['shulai']) && !empty($param['shulai'])){
//                if($param['shulai'] != $item['type']){
//                    $res = false;
//                }
//            }
//            if(isset($param['sericnum']) && !empty($param['sericnum'])){
//                if(strpos($item['sericnum'], $param['sericnum']) === false){
//                    $res = false;
//                }
//            }
//
//            if($res){
//                $list[] = $item;
//            }
//        }
//
//        $data['data'] = $list;
        $data['butauth'] = (new MenuService())->get_butauth('hou_chi_kou_qifei');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function dong_kou_qifei(Request $request){
        if($request->isPost()){
            $param = $request->post();

            $ids = $param['ids'];
            unset($param['ids']);
            unset($param['token']);

            Db::startTrans();
            try {
                HouKouQifeiModel::where('id','in',$ids)->update($param);
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['donger'] = $this->userinfo['realname'];
            $data['dongdt'] = date('Y-m-d');
             return outputJson(2000,lang('code_2000'), $data);
        }
    }


    public function list_hou_jie_tai(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['jie.deleted_user','=', 0];
        $where[] = ['jie.comid','=',$this->userinfo['comid']];
        $where[] = ['jie.isturn','=',1];
        $where[] = ['jie.status','=',1];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['jie.jieer','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['jieer']) && !empty($param['jieer'])){
            $where[] = ['jie.jieer','like', '%'.$param['jieer'].'%'];
        }

        $data['data'] = HouJieModel::alias('jie')
            ->field('jie.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('hou_jie', 'jie'))
            ->order('jie.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['fangtype_name'] = (new OptService())->get_base_showname('cai_jie_fangtype', $item['fangtype']);
                $item['state_name'] = (new ContractService())->get_contract_cai_state_name_tag($item['id'], $item['state']);
                $item['state_name_wen'] = (new ContractService())->get_contract_cai_state_name($item['id'], $item['state']);
                $item = (new HouService())->get_jieshuju($item);
                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }

    public function list_hou_jie(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['jie.deleted_user','=', 0];
        $where[] = ['jie.comid','=',$this->userinfo['comid']];
        $where[] = ['jie.isturn','=',1];
        $where[] = ['jie.status','in',[0,1]];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['jie.chuer|jie.jieer|jier.daner','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['chuer']) && !empty($param['chuer'])){
            $where[] = ['jie.chuer','like', '%'.$param['chuer'].'%'];
        }
        if(isset($param['jieer']) && !empty($param['jieer'])){
            $where[] = ['jie.jieer','like', '%'.$param['jieer'].'%'];
        }
        if(isset($param['daner']) && !empty($param['daner'])){
            $where[] = ['jie.daner','like', '%'.$param['daner'].'%'];
        }
        if(isset($param['state']) && $param['state'] != ''){
            $where[] = ['jie.state','=', $param['state']];
        }
        if(isset($param['qiandt']) && !empty($param['qiandt'])){
            $where[] = ['jie.qiandt','between', explode(' - ', $param['qiandt'])];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['jie.status','=', $param['status']];
        }

        $data['state'] = (new ContractService())->get_contract_cai_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';

        $data['data'] = HouJieModel::alias('jie')
            ->field('jie.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('hou_jie', 'jie'))
            ->order('jie.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['fangtype_name'] = (new OptService())->get_base_showname('cai_jie_fangtype', $item['fangtype']);
                $item['state_name'] = (new ContractService())->get_contract_cai_state_name_tag($item['id'], $item['state']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('hou_jie');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function shou_hou_jie(Request $request){
        if($request->isPost()){
            $param = $request->post();


            if($param['daomoney'] == ''){
                return outputJson(4000, '请输入放款到账金额');
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                HouJieModel::update($param);
                (new OptService())->relation_file($files,'hou_jie',$param['id']);
                (new OptService())->add_flow_log('收条', 'hou_jie', $param['id']);
                (new OptService())->add_log_list('收条', 'hou_jie', $param['id'], '借款登记');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = HouJieModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }
            $data['files'] = (new OptService())->get_file('hou_jie', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_hou_jie(Request $request){
        $param = $request->post();
        $data = HouJieModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            HouJieModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'hou_jie', $data['id'], '借款登记');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_hou_jie(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = HouJieModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['data']['fangtype_name'] = (new OptService())->get_base_showname('cai_jie_fangtype', $data['data']['fangtype']);
        $data['data']['state_name'] = (new ContractService())->get_contract_cai_state_name_tag($data['data']['id'], $data['data']['state']);
        $data['data']['state_name_wen'] = (new ContractService())->get_contract_cai_state_name($data['data']['id'], $data['data']['state']);

        $list = HouJieListModel::where(['jieid'=>$data['data']['id']])->select()->toArray();
        foreach ($list as &$item){
            $item['kuaninfo'] = (new FinanceService())->get_acckuan_info_bybillid($data['data']['billid'], $item['id']);
        }
        $data['list'] = $list;

        $tiao = HouTiaoModel::where(['jieid'=>$data['data']['id']])->select()->toArray();
        $data['tiao'] = $tiao;



        $qianhui = SuoHuiModel::where(['contractId'=>$data['data']['qysconid']])->select()->toArray();
        $data['qianhui'] = $qianhui;

        $data['files'] = (new OptService())->get_file('hou_jie', $data['data']['id']);
        $data['kuaninfo'] = (new FinanceService())->get_acckuan_info_bybillid($data['data']['billid']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function list_hou_shou(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['shou.deleted_user','=', 0];
        $where[] = ['shou.comid','=',$this->userinfo['comid']];
        $where[] = ['shou.isturn','=',1];
        $where[] = ['shou.status','in',[0,1]];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['jie.chuer|jie.jieer','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['chuer']) && !empty($param['chuer'])){
            $where[] = ['jie.chuer','like', '%'.$param['chuer'].'%'];
        }
        if(isset($param['jieer']) && !empty($param['jieer'])){
            $where[] = ['jie.jieer','like', '%'.$param['jieer'].'%'];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['shou.state','=', $param['state']];
        }
        if(isset($param['daodt']) && !empty($param['daodt'])){
            $where[] = ['shou.daodt','between', explode(' - ', $param['daodt'])];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['shou.status','=', $param['status']];
        }

        $data['state'] = (new ContractService())->get_contract_cai_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';

        $data['data'] = HouShouModel::alias('shou')
            ->field('shou.*,jie.chuer,jie.jieer,jie.money as jiemoney')
            ->join('hou_jie jie','jie.id=shou.jieid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('hou_shou', 'shou'))
            ->order('shou.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['state_name'] = (new ContractService())->get_contract_cai_state_name_tag($item['id'], $item['state']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('hou_shou');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_hou_shou(Request $request){
        $param = $request->post();
        $data = HouShouModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            HouShouModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'hou_shou', $data['id'], '借款收条');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_hou_shou(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = HouShouModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $jieinfo = HouJieModel::where(['id'=>$data['data']['jieid']])->find();
        $data['jieinfo'] = $jieinfo;

        $qianhui = SuoHuiModel::where(['contractId'=>$data['data']['qysconid']])->select()->toArray();
        $data['qianhui'] = $qianhui;

        $data['data']['state_name'] = (new ContractService())->get_contract_cai_state_name_tag($data['data']['id'], $data['data']['state']);
        $data['data']['state_name_wen'] = (new ContractService())->get_contract_cai_state_name($data['data']['id'], $data['data']['state']);

        $data['files'] = (new OptService())->get_file('hou_shou', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }



    public function list_hou_tiao(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['tiao.deleted_user','=', 0];
        $where[] = ['tiao.comid','=',$this->userinfo['comid']];
        $where[] = ['tiao.isturn','=',1];
        $where[] = ['tiao.status','in',[0,1]];

        if(isset($param['jieid']) && !empty($param['jieid'])){
            $where[] = ['tiao.jieid','=', $param['jieid']];
        }


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['jie.chuer|jie.jieer|jie.number','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['chuer']) && !empty($param['chuer'])){
            $where[] = ['jie.chuer','like', '%'.$param['chuer'].'%'];
        }
        if(isset($param['jieer']) && !empty($param['jieer'])){
            $where[] = ['jie.jieer','like', '%'.$param['jieer'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['jie.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['daodt']) && !empty($param['daodt'])){
            $where[] = ['tiao.daodt','between', explode(' - ', $param['daodt'])];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['tiao.status','=', $param['status']];
        }


        $data['data'] = HouTiaoModel::alias('tiao')
            ->field('tiao.*,jie.chuer,jie.jieer,jie.number,jie.money as jiemoney,jie.yuerate')
            ->join('hou_jie jie','jie.id=tiao.jieid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('hou_tiao', 'tiao'))
            ->order('tiao.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item = (new HouService())->get_tiaoinfo($item, '');
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('hou_tiao');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_hou_tiao(Request $request){
        $param = $request->post();
        $data = HouTiaoModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            HouTiaoModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'hou_tiao', $data['id'], '借款收条');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_hou_tiao(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = HouTiaoModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $jieinfo = HouJieModel::where(['id'=>$data['data']['jieid']])->find();
        if($jieinfo){
            //放款合计金额
            $jieinfo['fangfee'] = HouTiaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'status'=>1,'isturn'=>1])
                ->where(['jieid'=>$jieinfo['id']])->sum('money');
            $jieinfo['daifee'] = round($jieinfo['money'] - $jieinfo['fangfee'], 2);
        }
        $data['jieinfo'] = $jieinfo;


        $data['files'] = (new OptService())->get_file('hou_tiao', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }


    public function list_hou_dayshou(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['shou.deleted_user','=', 0];
        $where[] = ['shou.comid','=',$this->userinfo['comid']];
        $where[] = ['shou.isturn','=',1];
        $where[] = ['shou.status','in',[0,1]];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['shou.shouer|shou.zhai','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['shouer']) && !empty($param['shouer'])){
            $where[] = ['shou.shouer','like', '%'.$param['shouer'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['shou.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['shouqilei']) && $param['shouqilei'] != ''){
            $where[] = ['shou.shouqilei','=', $param['shouqilei']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['shou.status','=', $param['status']];
        }

        $data['shouqilei'] = (new OptService())->get_base_info('cai_shouqilei');
        $data['is_shouqilei'] = isset($param['shouqilei']) ? $param['shouqilei'] : '';


        $data['data'] = HouDayshouModel::alias('shou')
            ->field('shou.*,acc.name as accname,acc.accnum')
            ->leftJoin('account acc','acc.id=shou.accid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('hou_dayshou', 'shou'))
            ->order('shou.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['shouqilei_name'] = (new OptService())->get_base_showname('cai_shouqilei', $item['shouqilei']);
                $item['incdaotype_name'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);
                $item['accid_name'] = $item['accname'].'('.$item['accnum'].')';
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('hou_dayshou');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_hou_dayshou(Request $request){
        $param = $request->post();
        $data = HouDayshouModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            HouDayshouModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'hou_dayshou', $data['id'], '日常收款');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_hou_dayshou(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = HouDayshouModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }
        $data['data']['shouqilei_name'] = (new OptService())->get_base_showname('cai_shouqilei', $data['data']['shouqilei']);
        $data['data']['incdaotype_name'] = (new OptService())->get_base_showname('cai_income_incdaotype', $data['data']['incdaotype']);
        $data['account'] = AccountModel::get_info_by_pk($data['data']['accid']);

        $data['files'] = (new OptService())->get_file('hou_dayshou', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }


    public function detail_hou_dayzhi(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['zhi.deleted_user','=', 0];
        $where[] = ['zhi.comid','=',$this->userinfo['comid']];
        $where[] = ['zhi.isturn','=',1];
        $where[] = ['zhi.status','in',[0,1]];


        if(isset($param['listid']) && !empty($param['listid'])){
            $where[] = ['list.id','=', $param['listid']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['bill.sericnum|list.fuer','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['sericnum']) && !empty($param['sericnum'])){
            $where[] = ['bill.sericnum','like', '%'.$param['sericnum'].'%'];
        }
        if(isset($param['fuer']) && !empty($param['fuer'])){
            $where[] = ['list.fuer','like', '%'.$param['fuer'].'%'];
        }
        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['zhi.flowuser','like', '%'.$param['flowuser'].'%'];
        }
        if(isset($param['zhiqilei']) && $param['zhiqilei'] != ''){
            $where[] = ['list.zhiqilei','=', $param['zhiqilei']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['zhi.status','=', $param['status']];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $created_atArr = explode(' - ', $param['created_at']);
            $where[] = ['zhi.created_at','between', [$created_atArr[0].' 00:00:00', $created_atArr[1].' 23:59:59']];
        }

        $data['zhiqilei'] = (new OptService())->get_base_info('cai_zhiqilei', 2);
        $data['is_zhiqilei'] = isset($param['zhiqilei']) ? $param['zhiqilei'] : '';

        $list = [];
        $info = HouDayzhiListModel::alias('list')
            ->field('zhi.*,bill.sericnum,list.id as listid,list.zhiqilei,list.fuer,list.money as listmoney,
            list.explain as listexplain,list.incdaotype,list.bankzhang,list.banknum,list.bankname,list.difee')
            ->join('hou_dayzhi zhi','zhi.id=list.dayzhiid')
            ->leftJoin('flow_bill bill','bill.id=zhi.billid')
            ->where($where)
            ->order('zhi.id DESC')
            ->select()->toArray();
        foreach ($info as &$item){
            $item['zhiqilei_name'] = (new OptService())->get_base_showname('cai_zhiqilei', $item['zhiqilei']);
            $item['incdaotype_name'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);

            $item['yifufee'] = 0;
            $item['yifufee'] = HouDayzhiGuanModel::alias('guan')
                ->field('list.*')
                ->join('hou_dayzhi zhi','zhi.id=guan.dayzhiid')
                ->join('piao_day piao','piao.id=guan.piaoid')
                ->where(['zhi.comid'=>$this->userinfo['comid'],'zhi.deleted_user'=>0,'zhi.isturn'=>1])
                ->where('zhi.status','in',[0,1])
                ->where(['piao.comid'=>$this->userinfo['comid'],'piao.deleted_user'=>0,'piao.isturn'=>1])
                ->where('piao.status','in',[0,1])
                ->where(['guan.dayzhilistid'=>$item['listid']])
                ->sum('difee');

            if($item['yifufee'] <= 0){
                $item['statename'] = '未收票';
            }else{
                if($item['yifufee'] < $item['listmoney']){
                    $item['statename'] = '部分收票';
                }else{
                    $item['statename'] = '已收票';
                }
            }
            $item['daifufee'] = round($item['listmoney'] - $item['yifufee'], 2);


            if(isset($param['statename']) && !empty($param['statename'])){
                if($param['statename'] == $item['statename']){
                    $list[] = $item;
                }
            }else{
                $list[] = $item;
            }
        }

        $data['data'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function detail_hou_dayzhi222(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['zhi.deleted_user','=', 0];
        $where[] = ['zhi.comid','=',$this->userinfo['comid']];
        $where[] = ['zhi.isturn','=',1];
        $where[] = ['zhi.status','in',[0,1]];



        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['bill.sericnum|list.fuer','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['sericnum']) && !empty($param['sericnum'])){
            $where[] = ['bill.sericnum','like', '%'.$param['sericnum'].'%'];
        }
        if(isset($param['fuer']) && !empty($param['fuer'])){
            $where[] = ['list.fuer','like', '%'.$param['fuer'].'%'];
        }
        if(isset($param['zhiqilei']) && $param['zhiqilei'] != ''){
            $where[] = ['list.zhiqilei','=', $param['zhiqilei']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['zhi.status','=', $param['status']];
        }

        $data['zhiqilei'] = (new OptService())->get_base_info('cai_zhiqilei', 2);
        $data['is_zhiqilei'] = isset($param['zhiqilei']) ? $param['zhiqilei'] : '';

        $data['data'] = HouDayzhiListModel::alias('list')
            ->field('zhi.*,bill.sericnum,list.id as listid,list.zhiqilei,list.fuer,list.money as listmoney,
            list.explain as listexplain,list.incdaotype,list.bankzhang,list.banknum,list.bankname,list.difee')
            ->join('hou_dayzhi zhi','zhi.id=list.dayzhiid')
            ->leftJoin('flow_bill bill','bill.id=zhi.billid')
            ->where($where)
            ->order('zhi.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['zhiqilei_name'] = (new OptService())->get_base_showname('cai_zhiqilei', $item['zhiqilei']);
                $item['incdaotype_name'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);

                $item['yifufee'] = 0;
                $item['yifufee'] = HouDayzhiGuanModel::alias('guan')
                    ->field('list.*')
                    ->join('hou_dayzhi zhi','zhi.id=guan.dayzhiid')
                    ->join('piao_shou piao','piao.id=guan.piaoid')
                    ->where(['zhi.comid'=>$this->userinfo['comid'],'zhi.deleted_user'=>0,'zhi.isturn'=>1,'zhi.status'=>1])
                    ->where(['piao.comid'=>$this->userinfo['comid'],'piao.deleted_user'=>0,'piao.isturn'=>1,'piao.status'=>1])
                    ->where(['guan.dayzhilistid'=>$item['listid']])
                    ->sum('difee');

                if($item['yifufee'] <= 0){
                    $item['statename'] = '未收票';
                }else{
                    if($item['yifufee'] < $item['listmoney']){
                        $item['statename'] = '部分收票';
                    }else{
                        $item['statename'] = '已收票';
                    }
                }
                $item['daifufee'] = round($item['listmoney'] - $item['yifufee'], 2);
                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_hou_dayzhi(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['zhi.deleted_user','=', 0];
        $where[] = ['zhi.comid','=',$this->userinfo['comid']];
        $where[] = ['zhi.isturn','=',1];
        $where[] = ['zhi.status','in',[0,1]];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['zhi.zhai','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['zhi.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['zhi.status','=', $param['status']];
        }
        if(isset($param['fu_unit']) && !empty($param['fu_unit'])){
            $where[] = ['zhi.fu_unit','=', $param['fu_unit']];
        }
        if(isset($param['feilei']) && !empty($param['feilei'])){
            $where[] = ['zhi.feilei','=', $param['feilei']];
        }

        $data['fu_unit'] = (new UserService())->get_chaninfo_all();
        $data['is_fu_unit'] = isset($param['fu_unit']) ? $param['fu_unit'] : '';

        $data['feilei'] = (new OptService())->get_base_info('cai_dayzhifeilei');
        $data['is_feilei'] = isset($param['feilei']) ? $param['feilei'] : '';

        $data['data'] = HouDayzhiModel::alias('zhi')
            ->field('zhi.*,fu_unit.name as fu_unit_name')
            ->leftJoin('dept fu_unit','fu_unit.id=zhi.fu_unit')
            ->where($where)
            ->where((new MenuService())->get_miss_where('hou_dayzhi', 'zhi'))
            ->order('zhi.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['feilei_name'] = (new OptService())->get_base_showname('cai_dayzhifeilei', $item['feilei']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('hou_dayzhi');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function kuan_hou_dayzhi(Request $request){
        if($request->isPost()){
            $param = $request->post();

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);



            Db::startTrans();
            try {
                HouDayzhiModel::update($param);

                foreach($list as $vv){
                    HouDayzhiListModel::where(['id'=>$vv['id']])->update(['zhiqilei'=>$vv['zhiqilei']]);
                }
                (new OptService())->add_flow_log('编辑款项类型', 'hou_dayzhi', $param['id']);
                (new OptService())->add_log_list('编辑款项类型', 'hou_dayzhi', $param['id'], '日常付款');

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = HouDayzhiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['data']['fu_unit_info'] = (new UserService())->get_users_part_arr($data['data']['fu_unit'], 'dept');

            $data['feilei'] = (new OptService())->get_base_info('cai_dayzhifeilei');
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['zhiqilei'] = (new OptService())->get_base_info('cai_zhiqilei', 2);

            $list = HouDayzhiListModel::where(['dayzhiid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['incdaotype_name'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);
                $difeeList = HouDayzhiGuanModel::field('piaoid,piaolistid,difee')
                    ->where(['type'=>1,'dayzhiid'=>$item['dayzhiid'],'dayzhilistid'=>$item['id']])->select()->toArray();
                $item['difeeobj'] = json_encode($difeeList);
            }
            $data['list'] = $list;
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_hou_dayzhi(Request $request){
        $param = $request->post();
        $data = HouDayzhiModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            HouDayzhiModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'hou_dayzhi', $data['id'], '日常付款');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_hou_dayzhi(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = HouDayzhiModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['data']['feilei_name'] = (new OptService())->get_base_showname('cai_dayzhifeilei', $data['data']['feilei']);

        $data['data']['fu_unit_name'] = DeptModel::where(['id'=>$data['data']['fu_unit']])->value('name');

        $list = HouDayzhiListModel::where(['dayzhiid'=>$data['data']['id']])->select()->toArray();
        foreach ($list as &$item){
            $item['incdaotype_name'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);
            $item['zhiqilei_name'] = (new OptService())->get_base_showname('cai_zhiqilei', $item['zhiqilei']);
            $item['piaoid'] = 0;
            $exist = HouDayzhiGuanModel::alias('guan')
                ->field('guan.*')
                ->join('hou_dayzhi zhi','zhi.id=guan.dayzhiid')
                ->join('piao_day piao','piao.id=guan.piaoid')
                ->where(['guan.dayzhilistid'=>$item['id']])
                ->find();
            if($exist){
                $item['piaoid'] = $exist['piaoid'];
            }

            $item['kuaninfo'] = (new FinanceService())->get_acckuan_info_bybillid($data['data']['billid'], $item['id']);
        }
        $data['list'] = $list;

        $data['files'] = (new OptService())->get_file('hou_dayzhi', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }
    public function list_piao_day_dayzhi_guan(Request $request){
        $fuer = $request->post('fuer', '', 'string');
        if(!$fuer){
            return outputJson(2000,lang('code_2000'),['list'=>[]]);
        }

        $list = [];
        $chuList = PiaoDayListModel::alias('list')
            ->field('list.*')
            ->join('piao_day hou','hou.id=list.dayid')
            ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
            ->where(['list.xiaoer'=>$fuer])
            ->select()->toArray();

        foreach ($chuList as $item){
            $item['piaoid'] = $item['dayid'];
            $item['piaolistid'] = $item['id'];
            $item['qianpiaotype_name'] = (new OptService())->get_base_showname('cai_qianpiaotype', $item['qianpiaotype']);

            $item['yifufee'] = HouDayzhiGuanModel::alias('guan')
                ->field('list.*')
                ->join('hou_dayzhi zhi','zhi.id=guan.dayzhiid')
                ->join('piao_day piao','piao.id=guan.piaoid')
                ->where(['zhi.comid'=>$this->userinfo['comid'],'zhi.deleted_user'=>0,'zhi.isturn'=>1,'zhi.status'=>1])
                ->where(['piao.comid'=>$this->userinfo['comid'],'piao.deleted_user'=>0,'piao.isturn'=>1,'piao.status'=>1])
                ->where(['guan.piaolistid'=>$item['id']])
                ->sum('difee');
            if($item['yifufee'] <= 0){
                $item['statename'] = '未付款';
            }else{
                if($item['yifufee'] < $item['money']){
                    $item['statename'] = '部分付款';
                }else{
                    continue;
                }
            }
            $item['daifufee'] = round($item['money'] - $item['yifufee'], 2);
            $list[] = $item;
        }

        $data['list'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }




    public function list_kuai_hui(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['kuai.deleted_user','=', 0];
        $where[] = ['kuai.comid','=',$this->userinfo['comid']];
        $where[] = ['kuai.isturn','=',1];
        $where[] = ['kuai.status','=',1];


        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['kuai.enginid','=', $param['enginid']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|e.gofuer|kuai.yunnum','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['gofuer']) && !empty($param['gofuer'])){
            $where[] = ['e.gofuer','like', '%'.$param['gofuer'].'%'];
        }
        if(isset($param['yunnum']) && !empty($param['yunnum'])){
            $where[] = ['kuai.yunnum','like', '%'.$param['yunnum'].'%'];
        }
        if(isset($param['jidt']) && !empty($param['jidt'])){
            $where[] = ['kuai.jidt','between', explode(' - ', $param['jidt'])];
        }

        if(isset($param['futype']) && !empty($param['futype'])){
            $where[] = ['kuai.futype','=', $param['futype']];
        }
        if(isset($param['isgua']) && !empty($param['isgua'])){
            $where[] = ['kuai.isgua','=', $param['isgua']];
        }

        $list = [];
        $kuaitype = $request->post('kuaitype', 0, 'int');
        $kou_state = $request->post('kou_state', 0, 'int');

        if($kuaitype == 0 || $kuaitype == 1){
            $kuaijiInfo = KuaiJiModel::alias('kuai')
                ->field('kuai.*,e.name as enginname,e.gofuer')
                ->leftJoin('engin e','e.id=kuai.enginid')
                ->where($where)->where(['kuai.jistate'=>1])->select()->toArray();
            foreach ($kuaijiInfo as &$jivv){
                $jivv['kuaitype'] = 1;
                $jivv['kuaitype_name'] = '寄件';

                $jivv['jiername'] = UserModel::get_realname_by_pk($jivv['jier']);

                if($jivv['isgua'] == '是'){
                    $jivv['kou_state_name'] = '待收取';
                    $yikouHui = HouKouKuaifeiModel::alias('list')
                        ->join('hou_gonghui hou','hou.id=list.zhuid')
                        ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                        ->where(['list.type'=>1,'list.kuaitype'=>1,'list.kuaiid'=>$jivv['id']])
                        ->find();
                    if($yikouHui){
                        $jivv['kou_state_name'] = '已收取';
                    }else{
                        $yikouYu = HouKouKuaifeiModel::alias('list')
                            ->join('hou_gongyu hou','hou.id=list.zhuid')
                            ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                            ->where(['list.type'=>2,'list.kuaitype'=>1,'list.kuaiid'=>$jivv['id']])
                            ->find();
                        if($yikouYu){
                            $jivv['kou_state_name'] = '已收取';
                        }
                    }
                }else{
                    $jivv['kou_state_name'] = '-';
                }


                if($kou_state == 1){
                    if($jivv['kou_state_name'] == '已收取'){
                        $list[] = $jivv;
                    }
                }elseif($kou_state == 2){
                    if($jivv['kou_state_name'] == '待收取'){
                        $list[] = $jivv;
                    }
                }else{
                    $list[] = $jivv;
                }
            }
        }

        if($kuaitype == 0 || $kuaitype == 2){
            $kuaishouInfo = KuaiShouModel::alias('kuai')
                ->field('kuai.*,e.name as enginname,e.gofuer')
                ->leftJoin('engin e','e.id=kuai.enginid')
                ->where($where)->select()->toArray();
            foreach ($kuaishouInfo as &$shouvv){
                $shouvv['kuaitype'] = 2;
                $shouvv['kuaitype_name'] = '收件';

                $shouvv['jiername'] = $shouvv['shouer'];
                $shouvv['shouer'] = UserModel::get_realname_by_pk($shouvv['jier']);

                if($shouvv['isgua'] == '是'){
                    $shouvv['kou_state_name'] = '待收取';
                    $yikouHui = HouKouKuaifeiModel::alias('list')
                        ->join('hou_gonghui hou','hou.id=list.zhuid')
                        ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                        ->where(['list.type'=>1,'list.kuaitype'=>2,'list.kuaiid'=>$shouvv['id']])
                        ->find();
                    if($yikouHui){
                        $shouvv['kou_state_name'] = '已收取';
                    }else{
                        $yikouYu = HouKouKuaifeiModel::alias('list')
                            ->join('hou_gongyu hou','hou.id=list.zhuid')
                            ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                            ->where(['list.type'=>2,'list.kuaitype'=>2,'list.kuaiid'=>$shouvv['id']])
                            ->find();
                        if($yikouYu){
                            $shouvv['kou_state_name'] = '已收取';
                        }
                    }
                }else{
                    $shouvv['kou_state_name'] = '-';
                }


                if($kou_state == 1){
                    if($shouvv['kou_state_name'] == '已收取'){
                        $list[] = $shouvv;
                    }
                }elseif($kou_state == 2){
                    if($shouvv['kou_state_name'] == '待收取'){
                        $list[] = $shouvv;
                    }
                }else{
                    $list[] = $shouvv;
                }
            }
        }

        $subSort = array_column($list,'jidt');
        array_multisort($subSort,SORT_DESC,$list);


        $data['data'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_kuai_ji(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['ji.deleted_user','=', 0];
        $where[] = ['ji.comid','=',$this->userinfo['comid']];
        $where[] = ['ji.isturn','=',1];
        $where[] = ['ji.status','in',[0,1]];


        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['ji.enginid','=', $param['enginid']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|e.gofuer|jier.realname|ji.shouer|ji.shoutel|ji.nei|ji.yunnum|ji.flowuser','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['gofuer']) && !empty($param['gofuer'])){
            $where[] = ['e.gofuer','like', '%'.$param['gofuer'].'%'];
        }
        if(isset($param['jiername']) && !empty($param['jiername'])){
            $where[] = ['jier.realname','like', '%'.$param['jiername'].'%'];
        }
        if(isset($param['shouer']) && !empty($param['shouer'])){
            $where[] = ['ji.shouer','like', '%'.$param['shouer'].'%'];
        }
        if(isset($param['shoutel']) && !empty($param['shoutel'])){
            $where[] = ['ji.shoutel','like', '%'.$param['shoutel'].'%'];
        }
        if(isset($param['nei']) && !empty($param['nei'])){
            $where[] = ['ji.nei','like', '%'.$param['nei'].'%'];
        }
        if(isset($param['yunnum']) && !empty($param['yunnum'])){
            $where[] = ['ji.yunnum','like', '%'.$param['yunnum'].'%'];
        }
        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['ji.flowuser','like', '%'.$param['flowuser'].'%'];
        }
        if(isset($param['jidt']) && !empty($param['jidt'])){
            $where[] = ['ji.jidt','between', explode(' - ', $param['jidt'])];
        }


        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['ji.status','=', $param['status']];
        }
        if(isset($param['jistate']) && $param['jistate'] != ''){
            $where[] = ['ji.jistate','=', $param['jistate']];
        }
        if(isset($param['futype']) && !empty($param['futype'])){
            $where[] = ['ji.futype','=', $param['futype']];
        }
        if(isset($param['isgua']) && !empty($param['isgua'])){
            $where[] = ['ji.isgua','=', $param['isgua']];
        }

        $data['data'] = KuaiJiModel::alias('ji')
            ->field('ji.*,e.name as enginname,e.gofuer,jier.realname as jiername')
            ->leftJoin('engin e','e.id=ji.enginid')
            ->join('user jier','jier.id=ji.jier')
            ->where($where)
            ->where((new MenuService())->get_miss_where('kuai_ji', 'ji'))
            ->order('ji.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['jistate_name'] = $item['jistate'] > 0 ? '已寄件' : '待寄件';
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('kuai_ji');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function edit_kuai_ji(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['jier'])){
                return outputJson(4000, '请选择寄件人');
            }
            if(empty($param['shouer'])){
                return outputJson(4000, '请输入收件人');
            }
            if(empty($param['shoutel'])){
                return outputJson(4000, '请输入收件人电话');
            }
            if(empty($param['shouaddr'])){
                return outputJson(4000, '请输入收件地址');
            }
            if(empty($param['futype'])){
                return outputJson(4000, '请选择付款方式');
            }
            if(empty($param['isgua'])){
                return outputJson(4000, '请选择是否工程挂账');
            }
            if($param['isgua'] == '是'){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择工程名称');
                }
            }
            if(empty($param['nei'])){
                return outputJson(4000, '请输入快件内容');
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                KuaiJiModel::update($param);


                (new OptService())->relation_file($files,'kuai_ji',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'kuai_ji', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'kuai_ji', $param['id'], '快递寄件');

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = KuaiJiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
            $data['jier_info'] = (new UserService())->get_users_part_arr($data['data']['jier'], 'user');
            $data['files'] = (new OptService())->get_file('kuai_ji', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_kuai_ji(Request $request){
        $param = $request->post();
        $data = KuaiJiModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            KuaiJiModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'kuai_ji', $data['id'], '快递寄件');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_kuai_ji(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = KuaiJiModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
        $data['data']['jier_name'] = UserModel::get_realname_by_pk($data['data']['jier']);

        $data['files'] = (new OptService())->get_file('kuai_ji', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }
    public function fa_kuai_ji(Request $request){
        if($request->isPost()){
            $param = $request->post();


            if(empty($param['yunnum'])){
                return outputJson(4000, '请输入运单号');
            }
            if(empty($param['jidt'])){
                return outputJson(4000, '请选择寄件日期');
            }
            if(empty($param['kuaifee'])){
                return outputJson(4000, '请输入快递费用');
            }

            $param['jistate'] = 1;
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                KuaiJiModel::update($param);
                (new OptService())->relation_file($files,'kuai_ji',$param['id']);

                (new OptService())->add_flow_log('快递寄件', 'kuai_ji', $param['id'],'快递寄件');
                (new OptService())->add_log_list('快递寄件', 'kuai_ji', $param['id'], '快递寄件');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = KuaiJiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }
            $data['data']['jier_name'] = UserModel::get_realname_by_pk($data['data']['jier']);
            $data['files'] = (new OptService())->get_file('kuai_ji', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function list_kuai_shou(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['shou.deleted_user','=', 0];
        $where[] = ['shou.comid','=',$this->userinfo['comid']];
        $where[] = ['shou.isturn','=',1];
        $where[] = ['shou.status','in',[0,1]];


        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['shou.enginid','=', $param['enginid']];
        }


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|e.gofuer|jier.realname|shou.shouer|shou.nei|shou.yunnum|shou.flowuser','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['gofuer']) && !empty($param['gofuer'])){
            $where[] = ['e.gofuer','like', '%'.$param['gofuer'].'%'];
        }
        if(isset($param['jiername']) && !empty($param['jiername'])){
            $where[] = ['jier.realname','like', '%'.$param['jiername'].'%'];
        }
        if(isset($param['shouer']) && !empty($param['shouer'])){
            $where[] = ['shou.shouer','like', '%'.$param['shouer'].'%'];
        }
        if(isset($param['nei']) && !empty($param['nei'])){
            $where[] = ['shou.nei','like', '%'.$param['nei'].'%'];
        }
        if(isset($param['yunnum']) && !empty($param['yunnum'])){
            $where[] = ['shou.yunnum','like', '%'.$param['yunnum'].'%'];
        }
        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['shou.flowuser','like', '%'.$param['flowuser'].'%'];
        }
        if(isset($param['jidt']) && !empty($param['jidt'])){
            $where[] = ['shou.jidt','between', explode(' - ', $param['jidt'])];
        }


        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['shou.status','=', $param['status']];
        }
        if(isset($param['futype']) && !empty($param['futype'])){
            $where[] = ['shou.futype','=', $param['futype']];
        }
        if(isset($param['isgua']) && !empty($param['isgua'])){
            $where[] = ['shou.isgua','=', $param['isgua']];
        }


        $data['data'] = KuaiShouModel::alias('shou')
            ->field('shou.*,e.name as enginname,e.gofuer,jier.realname as jiername')
            ->leftJoin('engin e','e.id=shou.enginid')
            ->join('user jier','jier.id=shou.jier')
            ->where($where)
            ->where((new MenuService())->get_miss_where('kuai_shou', 'shou'))
            ->order('shou.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('kuai_shou');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function edit_kuai_shou(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['jier'])){
                return outputJson(4000, '请选择收件人');
            }
            if(empty($param['shouer'])){
                return outputJson(4000, '请输入寄件人');
            }
            if(empty($param['futype'])){
                return outputJson(4000, '请选择付款方式');
            }
            if(empty($param['isgua'])){
                return outputJson(4000, '请选择是否工程挂账');
            }
            if($param['isgua'] == '是'){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择工程名称');
                }
            }
            if(empty($param['jidt'])){
                return outputJson(4000, '请选择收件日期');
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                KuaiShouModel::update($param);


                (new OptService())->relation_file($files,'kuai_shou',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'kuai_shou', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'kuai_shou', $param['id'], '快递收件');

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = KuaiShouModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);

            $data['jier_info'] = (new UserService())->get_users_part_arr($data['data']['jier'], 'user');
            $data['files'] = (new OptService())->get_file('kuai_shou', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_kuai_shou(Request $request){
        $param = $request->post();
        $data = KuaiShouModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            KuaiShouModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'shou', $data['id'], '快递收件');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_kuai_shou(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = KuaiShouModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
        $data['data']['jier_name'] = UserModel::get_realname_by_pk($data['data']['jier']);

        $data['files'] = (new OptService())->get_file('kuai_shou', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }

    //添加 合同编号 查询 之后
    public function list_doss_lan(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.isturn','=',1];
        $where[] = ['e.status','=',1];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|e.gofuer|e.gongzi|e.gongnei|e.jiaer','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['gofuer']) && !empty($param['gofuer'])){
            $where[] = ['e.gofuer','like', '%'.$param['gofuer'].'%'];
        }
        if(isset($param['shinumber']) && !empty($param['shinumber'])){
            $where[] = ['e.number','like', '%'.$param['shinumber'].'%'];
        }
        if(isset($param['gui_unit']) && !empty($param['gui_unit'])){
            $where[] = ['e.gui_unit','=', $param['gui_unit']];
        }

        if(isset($param['gongzi']) && !empty($param['gongzi'])){
            $where[] = ['e.gongzi','like', '%'.$param['gongzi'].'%'];
        }
        if(isset($param['gongnei']) && !empty($param['gongnei'])){
            $where[] = ['e.gongnei','like', '%'.$param['gongnei'].'%'];
        }
        if(isset($param['jiaer']) && !empty($param['jiaer'])){
            $where[] = ['e.jiaer','like', '%'.$param['jiaer'].'%'];
        }
        if(isset($param['windt']) && !empty($param['windt'])){
            $where[] = ['e.windt','between', explode(' - ', $param['windt'])];
        }

        $data['gui_unit'] = (new UserService())->get_chaninfo_all();
        $data['is_gui_unit'] = isset($param['gui_unit']) ? $param['gui_unit'] : '';




        $data['data'] = EnginModel::alias('e')
            ->field('e.*,gui_unit.name as gui_unit_name')
            ->leftJoin('dept gui_unit','gui_unit.id=e.gui_unit')
            ->where($where)
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['shiinfo'] = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$item['id'],'deleted_user'=>0])
                    ->where(['isturn'=>1,'status'=>1])->find();

                $item['gofutel'] = DealcomModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
                    ->where(['name'=>$item['gofuer']])->value('liantel');

                $item['gofuerjitel'] = $item['gofuer'].$item['gofutel'];

                $item['shigongqi'] = '';
                if($item['shiinfo']){
                    $item['shigongqi'] = count_days($item['shiinfo']['startdt'], $item['shiinfo']['enddt']);
                }

                $item['cert_ji'] = (new CertService())->get_engin_one_zhi($item['id'],'技术负责人');
                $item['cert_xiang'] = (new CertService())->get_engin_one_zhi($item['id'],'项目经理');

                $item['bidinfo'] = BidModel::where(['id'=>$item['bidid']])->find();
                $item['shijiefee'] = ContractShiJieModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$item['id'],'deleted_user'=>0])
                    ->where(['isturn'=>1,'status'=>1])->sum('money');

                $shixuzheng = DossModel::where(['deleted_user'=>0,'enginid'=>$item['id'],'dangfen'=>3])->find();
                $item['shixuzheng'] = $shixuzheng ? '<span class="colorlv">有</span>' : '-';
                $zhongbeibiao = DossModel::where(['deleted_user'=>0,'enginid'=>$item['id'],'dangfen'=>4])->find();
                $item['zhongbeibiao'] = $zhongbeibiao ? '<span class="colorlv">有</span>' : '-';
                $gongdang = DossModel::where(['deleted_user'=>0,'enginid'=>$item['id'],'dangfen'=>10])->find();
                $item['gongdang'] = $gongdang ? '<span class="colorlv">有</span>' : '-';
                $zhurenjiang = DossModel::where(['deleted_user'=>0,'enginid'=>$item['id'],'dangfen'=>11])->find();
                $item['zhurenjiang'] = $zhurenjiang ? '<span class="colorlv">有</span>' : '-';

                $nonglvxian = DossModel::where(['deleted_user'=>0,'enginid'=>$item['id'],'dangfen'=>12])->find();
                $item['nonglvxian'] = $nonglvxian ? '<span class="colorlv">有</span>' : '-';
                $gongshangxian = DossModel::where(['deleted_user'=>0,'enginid'=>$item['id'],'dangfen'=>13])->find();
                $item['gongshangxian'] = $gongshangxian ? '<span class="colorlv">有</span>' : '-';
                $xiangshangxian = DossModel::where(['deleted_user'=>0,'enginid'=>$item['id'],'dangfen'=>14])->find();
                $item['xiangshangxian'] = $xiangshangxian ? '<span class="colorlv">有</span>' : '-';
                $yufubao = DossModel::where(['deleted_user'=>0,'enginid'=>$item['id'],'dangfen'=>15])->find();
                $item['yufubao'] = $yufubao ? '<span class="colorlv">有</span>' : '-';
                $lvbao = DossModel::where(['deleted_user'=>0,'enginid'=>$item['id'],'dangfen'=>16])->find();
                $item['lvbao'] = $lvbao ? '<span class="colorlv">有</span>' : '-';
                $nonggongbao = DossModel::where(['deleted_user'=>0,'enginid'=>$item['id'],'dangfen'=>17])->find();
                $item['nonggongbao'] = $nonggongbao ? '<span class="colorlv">有</span>' : '-';

                $zhongtongzhi = DossModel::where(['deleted_user'=>0,'enginid'=>$item['id'],'dangfen'=>1])->find();
                $item['zhongtongzhi'] = $zhongtongzhi ? '<span class="colorlv">有</span>' : '-';
                $jiangongtong = DossModel::where(['deleted_user'=>0,'enginid'=>$item['id'],'dangfen'=>2])->find();
                $item['jiangongtong'] = $jiangongtong ? '<span class="colorlv">有</span>' : '-';
                $kaibao = DossModel::where(['deleted_user'=>0,'enginid'=>$item['id'],'dangfen'=>5])->find();
                $item['kaibao'] = $kaibao ? '<span class="colorlv">有</span>' : '-';
                $junbao = DossModel::where(['deleted_user'=>0,'enginid'=>$item['id'],'dangfen'=>6])->find();
                $item['junbao'] = $junbao ? '<span class="colorlv">有</span>' : '-';
                $junyan = DossModel::where(['deleted_user'=>0,'enginid'=>$item['id'],'dangfen'=>7])->find();
                $item['junyan'] = $junyan ? '<span class="colorlv">有</span>' : '-';
                $juntuzhi = DossModel::where(['deleted_user'=>0,'enginid'=>$item['id'],'dangfen'=>8])->find();
                $item['juntuzhi'] = $juntuzhi ? '<span class="colorlv">有</span>' : '-';
                $jieshen = DossModel::where(['deleted_user'=>0,'enginid'=>$item['id'],'dangfen'=>9])->find();
                $item['jieshen'] = $jieshen ? '<span class="colorlv">有</span>' : '-';

                $item['zhi_have'] = '-';
                $item['zhi_shou'] = '-';
                $sealzhi = SealZhiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
                    ->where(['enginid'=>$item['id']])->where(Db::raw("find_in_set('项目专用章',yinlei)"))->order('created_at ASC')->find();
                if($sealzhi){
                    if($sealzhi['kedt']){
                        $item['zhi_have'] = '<span class="colorlv">有</span>';
                        if($sealzhi['shoudt'] && $sealzhi['shoudt'] != '0000-00-00'){
                            $item['zhi_shou'] = '已收回';
                        }else{
                            $item['zhi_shou'] = '未收回';
                        }
                    }
                }

                $item['nongkai'] = '否';
                $item['nongqi'] = '否';
                $item['jiankai'] = '否';
                $item['jianqi'] = '否';
                $kaoacc = KaoaccModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'enginid'=>$item['id']])->find();
                if($kaoacc){
                    $item['nongkai'] = $kaoacc['nongkai'];
                    $item['nongqi'] = $kaoacc['nongqi'];
                    $item['jiankai'] = $kaoacc['jiankai'];
                    $item['jianqi'] = $kaoacc['jianqi'];
                }

                //招标文件【49】	投标文件【50】	预算文件【130】	施工图纸【131】
                $files_49 = FileModel::where(['comid'=>$this->userinfo['comid'],'table'=>'engin','mid'=>$item['id'],'typeid'=>49])->find();
                $item['files_49'] = $files_49 ? '<span class="colorlv">有</span>' : '-';
                $files_50 = FileModel::where(['comid'=>$this->userinfo['comid'],'table'=>'engin','mid'=>$item['id'],'typeid'=>50])->find();
                $item['files_50'] = $files_50 ? '<span class="colorlv">有</span>' : '-';
                $files_130 = FileModel::where(['comid'=>$this->userinfo['comid'],'table'=>'engin','mid'=>$item['id'],'typeid'=>130])->find();
                $item['files_130'] = $files_130 ? '<span class="colorlv">有</span>' : '-';
                $files_131 = FileModel::where(['comid'=>$this->userinfo['comid'],'table'=>'engin','mid'=>$item['id'],'typeid'=>131])->find();
                $item['files_131'] = $files_131 ? '<span class="colorlv">有</span>' : '-';

                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_engin_chan_hui(Request $request){
        $param = $request->post();

        $where = [];
        $where[] = ['deleted_user','=', 0];
        $where[] = ['comid','=',$this->userinfo['comid']];
        $where[] = ['isturn','=',1];
        $where[] = ['status','=',1];

        $year = $request->param('year', date("Y"), 'int');
        $data['year'] = (new ContractService())->get_contract_yeararr();
        $data['is_year'] = $year;


        $deptArr = [];
        $one = DeptModel::field('id,name')->where(['comid'=>$this->userinfo['comid'],'pid'=>0])->find();
        $deptArr[] = $one;
        $two = DeptModel::field('id,name')->where(['comid'=>$this->userinfo['comid'],'pid'=>$one['id'],'type'=>2])->order('sort ASC')->select()->toArray();
        foreach ($two as $v){
            $deptArr[] = $v;
        }

        $list = [];
        foreach ($deptArr as $item){
            $item['year'] = $year;
            $item['allnum'] = EnginModel::where($where)->where(['gui_unit'=>$item['id']])->where('windt','between', [$year.'-01-01', $year.'-12-31'])->count();
            $item['allfee'] = EnginModel::where($where)->where(['gui_unit'=>$item['id']])->where('windt','between', [$year.'-01-01', $year.'-12-31'])->sum('winfee');

            for ($i=1; $i<=12; $i++){
                $first_day = $year.'-'.$i.'-01';
                $last_day = date('Y-m-t', strtotime($first_day));
                $item['month'.$i] = EnginModel::where($where)->where(['gui_unit'=>$item['id']])->where('windt','between', [$first_day, $last_day])->sum('winfee');
            }
            $list[] = $item;
        }

        $data['data'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }




    public function list_doss(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['doss.deleted_user','=', 0];
        $where[] = ['doss.comid','=',$this->userinfo['comid']];
        if(isset($param['isfei']) && $param['isfei'] != ''){
            $where[] = ['doss.isfei','=', $param['isfei']];
        }else{
            $where[] = ['doss.isfei','=',0];
        }

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['doss.enginid','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|doss.number|doss.name|doss.xianger|doss.danglei|guier.realname','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['doss.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['doss.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['xianger']) && !empty($param['xianger'])){
            $where[] = ['doss.xianger','like', '%'.$param['xianger'].'%'];
        }
        if(isset($param['danglei']) && !empty($param['danglei'])){
            $where[] = ['doss.danglei','like', '%'.$param['danglei'].'%'];
        }
        if(isset($param['guiername']) && !empty($param['guiername'])){
            $where[] = ['guier.realname','like', '%'.$param['guiername'].'%'];
        }
        if(isset($param['dangdt']) && !empty($param['dangdt'])){
            $where[] = ['doss.dangdt','between', explode(' - ', $param['dangdt'])];
        }
        if(isset($param['guidt']) && !empty($param['guidt'])){
            $where[] = ['doss.guidt','between', explode(' - ', $param['guidt'])];
        }
        if(isset($param['dangfen']) && !empty($param['dangfen'])){
            $where[] = ['doss.dangfen','=', $param['dangfen']];
        }

        $data['dangfen'] = (new OptService())->get_base_info('doss_dangfen');
        $data['is_dangfen'] = isset($param['dangfen']) ? $param['dangfen'] : '';

        $data['data'] = DossModel::alias('doss')
            ->field('doss.*,guier.realname as guiername,e.name as enginname')
            ->join('user guier','guier.id=doss.guier')
            ->leftJoin('engin e','e.id=doss.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('doss', 'doss'))
            ->order('doss.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['dangfen_name'] = (new OptService())->get_base_showname('doss_dangfen', $item['dangfen']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('doss');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function yiinfo_doss(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['doss.deleted_user','=', 0];
        $where[] = ['doss.comid','=',$this->userinfo['comid']];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['doss.enginid','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|doss.number|doss.name|doss.xianger|doss.danglei|guier.realname','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['doss.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['doss.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['xianger']) && !empty($param['xianger'])){
            $where[] = ['doss.xianger','like', '%'.$param['xianger'].'%'];
        }
        if(isset($param['danglei']) && !empty($param['danglei'])){
            $where[] = ['doss.danglei','like', '%'.$param['danglei'].'%'];
        }
        if(isset($param['guiername']) && !empty($param['guiername'])){
            $where[] = ['guier.realname','like', '%'.$param['guiername'].'%'];
        }
        if(isset($param['dangdt']) && !empty($param['dangdt'])){
            $where[] = ['doss.dangdt','between', explode(' - ', $param['dangdt'])];
        }
        if(isset($param['guidt']) && !empty($param['guidt'])){
            $where[] = ['doss.guidt','between', explode(' - ', $param['guidt'])];
        }
        if(isset($param['dangfen']) && !empty($param['dangfen'])){
            $where[] = ['doss.dangfen','=', $param['dangfen']];
        }

        $data['dangfen'] = (new OptService())->get_base_info('doss_dangfen');
        $data['is_dangfen'] = isset($param['dangfen']) ? $param['dangfen'] : '';

        $data['data'] = DossJiaoModel::alias('doss')
            ->field('doss.*,guier.realname as guiername,e.name as enginname')
            ->join('user guier','guier.id=doss.guier')
            ->leftJoin('engin e','e.id=doss.enginid')
            ->where($where)
            ->order('doss.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){

                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_doss(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['dangfen'])){
                return outputJson(4000, '请选择档案分类');
            }
            if(empty($param['name'])){
                return outputJson(4000, '请输入档案名称');
            }
            if(empty($param['danglei'])){
                return outputJson(4000, '请选择档案类型');
            }
            if(empty($param['guinum'])){
                return outputJson(4000, '请输入归档份数');
            }
            if(empty($param['guidt'])){
                return outputJson(4000, '请输入归档日期');
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $jiaoid = $request->param('jiaoid');
            $dangtype = $request->param('dangtype');
            unset($param['jiaoid']);
            unset($param['dangtype']);

            Db::startTrans();
            try {

                if($jiaoid){
                    if($dangtype == 1){
                        DossJiaoModel::where(['id'=>$jiaoid])->update(['isjie'=>1]);
                    }elseif($dangtype == 2){
                        CossJiaoModel::where(['id'=>$jiaoid])->update(['isjie'=>1]);
                    }elseif($dangtype == 3){
                        XossJiaoModel::where(['id'=>$jiaoid])->update(['isjie'=>1]);
                    }
                }

                $id = DossModel::insertGetId($param);

                (new EnginRelevan())->vate_doss_number($id);

                (new OptService())->relation_file($files,'doss',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'doss', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'doss', $id, '工程档案库');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $dangtype = $request->get('dangtype',0,'int');
            $jiaoid = $request->get('jiaoid',0,'int');

            $fileArr = [];
            if($dangtype == 1){
                $data['jiaoinfo'] = DossJiaoModel::where(['id'=>$jiaoid])->find();
                if($data['jiaoinfo']){
                    $data['jiaoinfo']['engininfo'] = (new EnginService())->get_engin_showinfo($data['jiaoinfo']['enginid']);
                }
                $fileArr = FileModel::where(['table'=>'doss','mid'=>$data['jiaoinfo']['yiid'],'comid'=>$this->userinfo['comid']])->select()->toArray();
            }elseif($dangtype == 2){
                $data['jiaoinfo'] = CossJiaoModel::where(['id'=>$jiaoid])->find();
                if($data['jiaoinfo']){
                    $data['jiaoinfo']['engininfo'] = (new EnginService())->get_engin_showinfo($data['jiaoinfo']['enginid']);
                }
                $fileArr = FileModel::where(['table'=>'coss','mid'=>$data['jiaoinfo']['yiid'],'comid'=>$this->userinfo['comid']])->select()->toArray();
            }elseif($dangtype == 3){
                $data['jiaoinfo'] = XossJiaoModel::where(['id'=>$jiaoid])->find();
                if($data['jiaoinfo']){
                    $data['jiaoinfo']['engininfo'] = (new EnginService())->get_engin_showinfo($data['jiaoinfo']['enginid']);
                }
                $fileArr = FileModel::where(['table'=>'xoss','mid'=>$data['jiaoinfo']['yiid'],'comid'=>$this->userinfo['comid']])->select()->toArray();
            }
            FileModel::where(['table'=>'doss','mid'=>-$this->userinfo['id'],'comid'=>$this->userinfo['comid']])->delete();
            $typeid = FiletypeModel::where(['comid'=>$this->userinfo['comid'],'table'=>'doss','name'=>'相关附件'])->value('id');
            foreach ($fileArr as $fff){
                $ins = $fff;
                unset($ins['id']);
                $ins['table'] = 'doss';
                $ins['typeid'] = $typeid;
                $ins['mid'] = -$this->userinfo['id'];
                $ins['created_user'] = $this->userinfo['id'];
                $ins['created_at'] = date("Y-m-d H:i:s");
                $ins_id = FileModel::insertGetId($ins);
                $encpath =  encrypt($ins_id, 'E', Config::get('service.info_key'));
                if(FileModel::where(['id'=>$ins_id])->update(['encpath'=>$encpath]) === false){
                    return outputJson(4000,"附件处理失败,请重新操作");
                }
            }
            $data['files'] = (new OptService())->get_file('doss', -$this->userinfo['id']);


            $enginid = $request->get('enginid',0,'int');
            $data['engininfo'] = (new EnginService())->get_engin_showinfo($enginid);


            $data['guier_info'] = (new UserService())->get_users_part_arr($this->userinfo['id'], 'user');
            $data['dangfen'] = (new OptService())->get_base_info('doss_dangfen');
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_doss(Request $request){
        if($request->isPost()){
            $param = $request->post();


            if(empty($param['dangfen'])){
                return outputJson(4000, '请选择档案分类');
            }
            if(empty($param['name'])){
                return outputJson(4000, '请输入档案名称');
            }
            if(empty($param['danglei'])){
                return outputJson(4000, '请选择档案类型');
            }
            if(empty($param['guinum'])){
                return outputJson(4000, '请输入归档份数');
            }
            if(empty($param['guidt'])){
                return outputJson(4000, '请输入归档日期');
            }
            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                DossModel::update($param);
                (new OptService())->relation_file($files,'doss',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'doss', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'doss', $param['id'], '工程档案库');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = DossModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }
            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
            $data['data']['guier_info'] = (new UserService())->get_users_part_arr($data['data']['guier'], 'user');
            $data['dangfen'] = (new OptService())->get_base_info('doss_dangfen');
            $data['files'] = (new OptService())->get_file('doss', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function jiao_doss(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['yier'])){
                return outputJson(4000, '请选择移交人');
            }
            if(empty($param['yidt'])){
                return outputJson(4000, '请选择移交日期');
            }
            if(empty($param['yinum'])){
                return outputJson(4000, '请输入移交份数');
            }
            if(empty($param['jieer'])){
                return outputJson(4000, '请选择通知人');
            }


            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            unset($param['token']);

            $danginfo = DossModel::where(['id'=>$param['yiid']])->find();
            $param['jiaoqian'] = 1;
            $param['enginid'] = $danginfo['enginid'];
            $param['number'] = $danginfo['number'];
            $param['dangfen'] = $danginfo['dangfen'];
            $param['dangfen_name'] = (new OptService())->get_base_showname('doss_dangfen', $danginfo['dangfen']);
            $param['name'] = $danginfo['name'];
            $param['dangdt'] = $danginfo['dangdt'];
            $param['xianger'] = $danginfo['xianger'];
            $param['shefee'] = $danginfo['shefee'];
            $param['yeshu'] = $danginfo['yeshu'];
            $param['runum'] = $danginfo['runum'];
            $param['guinum'] = $danginfo['guinum'];
            $param['guidt'] = $danginfo['guidt'];
            $param['guier'] = $danginfo['guier'];
            $param['explain'] = $danginfo['explain'];

            Db::startTrans();
            try {
                $id = DossJiaoModel::insertGetId($param);

                //档案移交提醒消息：[来源]移交档案[档案名称]至[移交至]，请及时接收。
                $jiaohouName = "";
                if($param['jiaohou'] == 1){
                    $jiaohouName = '工程档案库';
                }elseif($param['jiaohou'] == 2){
                    $jiaohouName = '财务档案库';
                }elseif($param['jiaohou'] == 3){
                    $jiaohouName = '行政档案库';
                }
                $mess = "[工程档案库]移交档案[".$param['name']."]至[".$jiaohouName."]，请及时接收。";
                (new OptService())->add_todo($param['jieer'], '档案移交',$mess, $id, 'doss_jiao');

                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'doss_jiao', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'doss_jiao', $id, '工程档案移交');

                (new OptService())->add_flow_log('移交', 'doss', $param['yiid']);
                (new OptService())->add_log_list('移交', 'doss', $param['yiid'], '工程档案库');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = DossModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }
            $data['data']['yidt'] = date("Y-m-d");
            $data['data']['yier_info'] = (new UserService())->get_users_part_arr($this->userinfo['id'], 'user');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function fei_doss(Request $request){
        if($request->isPost()){
            $param = $request->post();


            if(empty($param['feier'])){
                return outputJson(4000, '请选择作废人');
            }
            if(empty($param['feidt'])){
                return outputJson(4000, '请选择作废日期');
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            unset($param['token']);

            if(isset($param['tongtype'])){
                $tongtype = $param['tongtype'];
                unset($param['tongtype']);

                foreach ($tongtype as $vvv){
                    if($vvv == 1){$param['fei_gong'] = 1;}
                    if($vvv == 2){$param['fei_cai'] = 1;}
                    if($vvv == 3){$param['fei_xing'] = 1;}
                }
            }

            $param['isfei'] = 1;


            Db::startTrans();
            try {
                DossModel::update($param);
                (new OptService())->add_flow_log('作废', 'doss', $param['id']);
                (new OptService())->add_log_list('作废', 'doss', $param['id'], '工程档案库');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = DossModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }
            $data['data']['feidt'] = date("Y-m-d");
            $data['data']['feier_info'] = (new UserService())->get_users_part_arr($this->userinfo['id'], 'user');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function qufei_doss(Request $request){
        $param = $request->post();
        $data = DossModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            DossModel::where(['id'=>$param['id']])->update([
                'isfei'=>0,
                'fei_gong'=>0,
                'fei_cai'=>0,
                'fei_xing'=>0,
                'feier'=>0,
                'feidt'=>'',
                'feishuo'=>'',
            ]);

            (new OptService())->add_flow_log('取消作废', 'doss', $param['id']);
            (new OptService())->add_log_list('取消作废', 'doss', $param['id'], '工程档案库');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function del_doss(Request $request){
        $param = $request->post();
        $data = DossModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            DossModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);

            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'doss', $data['id'], '工程档案库');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_doss(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = DossModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }
        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
        $data['data']['guier_name'] = UserModel::get_realname_by_pk($data['data']['guier']);
        $data['data']['dangfen_name'] = (new OptService())->get_base_showname('doss_dangfen', $data['data']['dangfen']);
        $data['files'] = (new OptService())->get_file('doss', $data['data']['id']);
        return outputJson(2000,lang('code_2000'), $data);
    }
    public function down_doss(Request $request){
        $id = $request->param('id', 0, 'int');
        $data['data'] = DossModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }


        $id_arr = FileModel::where(['table'=>'doss','mid'=>$data['data']['id']])->column('id');
        $filename = $data['data']['name'];

        if(!$id_arr){
            return outputJson(4000, '暂无附件下载');
        }

        Db::startTrans();
        try {
            $file_dir = 'tmp_zip/';
            $filename = $filename.'.zip';

            if(file_exists($file_dir.$filename)){
                unlink($file_dir.$filename);
            }

            $zip = new \ZipArchive();
            $res = $zip->open($file_dir.$filename, $zip::CREATE);
            if ($res === TRUE) {
                foreach ($id_arr as $fileid) {
                    if($fileid < 1){
                        continue;
                    }
                    $_file = FileModel::where(['id'=>$fileid])->find();
                    if(!$_file || $_file['comid'] != $this->userinfo['comid']){
                        throw new Exception("操作失败");
                    }

                    $zip->addFile($_file['oripath'], substr($_file['oripath'],22));
                }
            }
            $zip->close();

        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'), $file_dir.$filename);
    }

    public function list_doss_jiao(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['dang.deleted_user','=', 0];
        $where[] = ['dang.comid','=',$this->userinfo['comid']];
        $where[] = ['dang.jiaohou','=',1];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|dang.number|dang.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['dang.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['dang.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['isjie']) && $param['isjie'] != ''){
            $where[] = ['dang.isjie','=', $param['isjie']];
        }

        $list = [];

        $cossinfo = CossJiaoModel::alias('dang')
            ->field('dang.*,e.name as enginname')
            ->leftJoin('engin e','e.id=dang.enginid')
            ->where($where)
            ->select()->toArray();
        foreach($cossinfo as $cossvv){
            $cossvv['dangtype'] = 2;
            $cossvv['yier_name'] = UserModel::get_realname_by_pk($cossvv['yier']);
            $cossvv['jieer_name'] = UserModel::get_realname_by_pk($cossvv['jieer']);
            $list[] = $cossvv;
        }


        $xossinfo = XossJiaoModel::alias('dang')
            ->field('dang.*,e.name as enginname')
            ->leftJoin('engin e','e.id=dang.enginid')
            ->where($where)
            ->select()->toArray();
        foreach($xossinfo as $xossvv){
            $xossvv['dangtype'] = 3;
            $xossvv['yier_name'] = UserModel::get_realname_by_pk($xossvv['yier']);
            $xossvv['jieer_name'] = UserModel::get_realname_by_pk($xossvv['jieer']);
            $list[] = $xossvv;
        }


        $dtSort = array_column($list,'yidt');
        array_multisort($dtSort,SORT_DESC, $list);
        $data['data'] = $list;

        $data['butauth'] = (new MenuService())->get_butauth('doss_jiao');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_doss_jiao(Request $request){
        $param = $request->post();
        $data = DossJiaoModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            DossJiaoModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'doss_jiao', $data['id'], '待接收档案');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_doss_jiao(Request $request){
        $dangtype = $request->post('dangtype', 0, 'int');
        $id = $request->post('id', 0, 'int');
        if($dangtype == 1){
            $data['data'] = DossJiaoModel::get_info_by_pk($id);
            $data['files'] = (new OptService())->get_file('doss', $data['data']['yiid']);
        }elseif($dangtype == 2){
            $data['data'] = CossJiaoModel::get_info_by_pk($id);
            $data['files'] = (new OptService())->get_file('coss', $data['data']['yiid']);
        }elseif($dangtype == 3){
            $data['data'] = XossJiaoModel::get_info_by_pk($id);
            $data['files'] = (new OptService())->get_file('xoss', $data['data']['yiid']);
        }
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }
        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);

        $data['data']['yier_name'] = UserModel::get_realname_by_pk($data['data']['yier']);
        $data['data']['jieer_name'] = UserModel::get_realname_by_pk($data['data']['jieer']);
        return outputJson(2000,lang('code_2000'), $data);
    }
    public function list_doss_fei(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['dang.deleted_user','=', 0];
        $where[] = ['dang.comid','=',$this->userinfo['comid']];
        $where[] = ['dang.isfei','=',1];
        $where[] = ['dang.fei_gong','=',1];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|dang.number|dang.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['dang.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['dang.name','like', '%'.$param['name'].'%'];
        }

        $list = [];

        $cossinfo = CossModel::alias('dang')
            ->field('dang.*,e.name as enginname')
            ->leftJoin('engin e','e.id=dang.enginid')
            ->where($where)
            ->select()->toArray();
        foreach($cossinfo as $cossvv){
            $cossvv['dangtype'] = 2;
            $cossvv['dangfen_name'] = (new OptService())->get_base_showname('coss_dangfen', $cossvv['dangfen']);
            $list[] = $cossvv;
        }


        $xossinfo = XossModel::alias('dang')
            ->field('dang.*,e.name as enginname')
            ->leftJoin('engin e','e.id=dang.enginid')
            ->where($where)
            ->select()->toArray();
        foreach($xossinfo as $xossvv){
            $xossvv['dangtype'] = 3;
            $xossvv['dangfen_name'] = (new OptService())->get_base_showname('xoss_dangfen', $xossvv['dangfen']);
            $list[] = $xossvv;
        }


        $dtSort = array_column($list,'feidt');
        array_multisort($dtSort,SORT_DESC, $list);
        $data['data'] = $list;

        $data['butauth'] = (new MenuService())->get_butauth('doss_fei');
        return outputJson(2000,lang('code_2000'),$data);
    }

    public function detail_doss_jie(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['jie.deleted_user','=', 0];
        $where[] = ['jie.comid','=',$this->userinfo['comid']];
        $where[] = ['jie.isturn','=',1];
        $where[] = ['jie.status','in',[0,1]];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['bill.sericnum|jieer.realname|doss.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['sericnum']) && !empty($param['sericnum'])){
            $where[] = ['bill.sericnum','like', '%'.$param['sericnum'].'%'];
        }
        if(isset($param['jieername']) && !empty($param['jieername'])){
            $where[] = ['jieer.realname','like', '%'.$param['jieername'].'%'];
        }
        if(isset($param['doss_name']) && !empty($param['doss_name'])){
            $where[] = ['doss.name','like', '%'.$param['doss_name'].'%'];
        }


        $data['data'] = DossJieListModel::alias('list')
            ->field('jie.*,jieer.realname as jieername,dept.name as jieerdept,
            list.id as listid,list.explain as list_explain,list.guidt,list.guier,list.state,list.num,guier.realname as guiername,
            doss.name as doss_name,doss.number as doss_number,doss.dangfen,e.name as enginname,bill.sericnum')
            ->join('doss_jie jie','jie.id=list.jieid')
            ->leftJoin('flow_bill bill','bill.id=jie.billid')
            ->leftJoin('doss doss','doss.id=list.dossid')
            ->leftJoin('engin e','e.id=doss.enginid')
            ->leftJoin('user jieer','jieer.id=jie.jieer')
            ->leftJoin('dept dept','dept.id=jieer.deptid')
            ->leftJoin('user guier','guier.id=list.guier')
            ->where($where)
            ->where((new MenuService())->get_miss_where('doss_jie', 'jie'))
            ->order('jie.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['dangfen_name'] = (new OptService())->get_base_showname('doss_dangfen', $item['dangfen']);
                $item['gui_state_name'] = $item['state'] > 0 ? '已归还' : '未归还';

                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('doss_jie_detail');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_doss_jie(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['jie.deleted_user','=', 0];
        $where[] = ['jie.comid','=',$this->userinfo['comid']];
        $where[] = ['jie.isturn','=',1];
        $where[] = ['jie.status','in',[0,1]];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['jieer.realname|jie.yong','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['jieername']) && !empty($param['jieername'])){
            $where[] = ['jieer.realname','like', '%'.$param['jieername'].'%'];
        }
        if(isset($param['yong']) && !empty($param['yong'])){
            $where[] = ['jie.yong','like', '%'.$param['yong'].'%'];
        }
        if(isset($param['yudt']) && !empty($param['yudt'])){
            $where[] = ['jie.yudt','between', explode(' - ', $param['yudt'])];
        }
        if(isset($param['state']) && $param['state'] != ''){
            $where[] = ['jie.state','=', $param['state']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['jie.status','=', $param['status']];
        }


        $data['data'] = DossJieModel::alias('jie')
            ->field('jie.*,jieer.realname as jieername,dept.name as deptname')
            ->leftJoin('user jieer','jieer.id=jie.jieer')
            ->leftJoin('dept dept','dept.id=jieer.deptid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('doss_jie', 'jie'))
            ->order('jie.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $listOneDossid = DossJieListModel::where(['jieid'=>$item['id']])->value('dossid');
                $item['jietuname'] = DossModel::where(['id'=>$listOneDossid])->value('name');
                $listNum = DossJieListModel::where(['jieid'=>$item['id']])->count();
                if($listNum > 1){
                    $item['jietuname'] .= '...等'.$listNum.'本';
                }
                $item['state_name'] = (new XingService())->get_doss_jie_state_name($item['state']);

                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('doss_jie');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_doss_jie(Request $request){
        $param = $request->post();
        $data = DossJieModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            DossJieModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'doss_jie', $data['id'], '工程档案借阅');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_doss_jie(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = DossJieModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $jieerinfo = UserModel::where(['id'=>$data['data']['jieer']])->find();
        $data['data']['jieer_name'] = $jieerinfo['realname'];
        $data['data']['dept_name'] = DeptModel::get_name_by_pk($jieerinfo['deptid']);

        $list = DossJieListModel::where(['jieid'=>$data['data']['id']])->select()->toArray();
        foreach ($list as &$item){
            $dossinfo = DossModel::where(['id'=>$item['dossid']])->find();
            if($dossinfo){
                $item['dangfen_name'] = (new OptService())->get_base_showname('doss_dangfen', $dossinfo['dangfen']);

                $item['name'] = $dossinfo['name'];
                $item['number'] = $dossinfo['number'];
                $item['enginname'] = EnginModel::where(['id'=>$dossinfo['enginid']])->value('name');
            }
            $item['guier_name'] = UserModel::get_realname_by_pk($item['guier']);
        }
        $data['list'] = $list;

        $data['files'] = (new OptService())->get_file('doss_jie', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }
    public function gui_doss_jie(Request $request){
        if($request->isPost()){
            $param = $request->post();


            if(empty($param['guidt'])){
                return outputJson(4000, '请选择归还日期');
            }
            if(empty($param['guier'])){
                return outputJson(4000, '请选择接收人');
            }

            $param['state'] = 1;
            unset($param['token']);

            $listinfo = DossJieListModel::where(['id'=>$param['id']])->find();



            Db::startTrans();
            try {
                DossJieListModel::update($param);

                $jie_state = 1;
                $nohuan = DossJieListModel::where(['jieid'=>$listinfo['jieid'],'state'=>0])->find();
                if(!$nohuan){
                    $jie_state = 2;
                }
                DossJieModel::where(['id'=>$listinfo['jieid']])->update(['state'=>$jie_state]);


                (new OptService())->add_flow_log('工程档案归还', 'doss_jie', $listinfo['id']);
                (new OptService())->add_log_list('工程档案归还', 'doss_jie', $listinfo['id'], '工程档案借阅');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = DossJieListModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }
            $data['dossinfo'] = DossModel::where(['id'=>$data['data']['dossid']])->find();

            if($data['data']['state'] == 0){
                $data['data']['guidt'] = date("Y-m-d");
                $data['data']['guier_info'] = (new UserService())->get_users_part_arr($this->userinfo['id'], 'user');
            }else{
                $data['data']['guier_info'] = (new UserService())->get_users_part_arr($data['data']['guier'], 'user');
            }
            return outputJson(2000,lang('code_2000'),$data);
        }
    }





    public function list_coss(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['coss.deleted_user','=', 0];
        $where[] = ['coss.comid','=',$this->userinfo['comid']];
        if(isset($param['isfei']) && $param['isfei'] != ''){
            $where[] = ['coss.isfei','=', $param['isfei']];
        }else{
            $where[] = ['coss.isfei','=',0];
        }


        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['coss.enginid','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|coss.number|coss.name|coss.xianger|coss.danglei|guier.realname','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['coss.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['coss.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['xianger']) && !empty($param['xianger'])){
            $where[] = ['coss.xianger','like', '%'.$param['xianger'].'%'];
        }
        if(isset($param['danglei']) && !empty($param['danglei'])){
            $where[] = ['coss.danglei','like', '%'.$param['danglei'].'%'];
        }
        if(isset($param['guiername']) && !empty($param['guiername'])){
            $where[] = ['guier.realname','like', '%'.$param['guiername'].'%'];
        }
        if(isset($param['dangdt']) && !empty($param['dangdt'])){
            $where[] = ['coss.dangdt','between', explode(' - ', $param['dangdt'])];
        }
        if(isset($param['guidt']) && !empty($param['guidt'])){
            $where[] = ['coss.guidt','between', explode(' - ', $param['guidt'])];
        }
        if(isset($param['dangfen']) && !empty($param['dangfen'])){
            $where[] = ['coss.dangfen','=', $param['dangfen']];
        }

        $data['dangfen'] = (new OptService())->get_base_info('coss_dangfen');
        $data['is_dangfen'] = isset($param['dangfen']) ? $param['dangfen'] : '';

        $data['data'] = CossModel::alias('coss')
            ->field('coss.*,guier.realname as guiername,e.name as enginname,e.number as enginnumber')
            ->join('user guier','guier.id=coss.guier')
            ->leftJoin('engin e','e.id=coss.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('coss', 'coss'))
            ->order('coss.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['dangfen_name'] = (new OptService())->get_base_showname('coss_dangfen', $item['dangfen']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('coss');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function yiinfo_coss(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['coss.deleted_user','=', 0];
        $where[] = ['coss.comid','=',$this->userinfo['comid']];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['coss.enginid','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|coss.number|coss.name|coss.xianger|coss.danglei|guier.realname','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['coss.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['coss.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['xianger']) && !empty($param['xianger'])){
            $where[] = ['coss.xianger','like', '%'.$param['xianger'].'%'];
        }
        if(isset($param['danglei']) && !empty($param['danglei'])){
            $where[] = ['coss.danglei','like', '%'.$param['danglei'].'%'];
        }
        if(isset($param['guiername']) && !empty($param['guiername'])){
            $where[] = ['guier.realname','like', '%'.$param['guiername'].'%'];
        }
        if(isset($param['dangdt']) && !empty($param['dangdt'])){
            $where[] = ['coss.dangdt','between', explode(' - ', $param['dangdt'])];
        }
        if(isset($param['guidt']) && !empty($param['guidt'])){
            $where[] = ['coss.guidt','between', explode(' - ', $param['guidt'])];
        }
        if(isset($param['dangfen']) && !empty($param['dangfen'])){
            $where[] = ['coss.dangfen','=', $param['dangfen']];
        }

        $data['dangfen'] = (new OptService())->get_base_info('coss_dangfen');
        $data['is_dangfen'] = isset($param['dangfen']) ? $param['dangfen'] : '';

        $data['data'] = CossJiaoModel::alias('coss')
            ->field('coss.*,guier.realname as guiername,e.name as enginname')
            ->join('user guier','guier.id=coss.guier')
            ->leftJoin('engin e','e.id=coss.enginid')
            ->where($where)
            ->order('coss.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_coss(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['dangfen'])){
                return outputJson(4000, '请选择档案分类');
            }
            if(empty($param['name'])){
                return outputJson(4000, '请输入档案名称');
            }
            if(empty($param['danglei'])){
                return outputJson(4000, '请选择档案类型');
            }
            if(empty($param['guinum'])){
                return outputJson(4000, '请输入归档份数');
            }
            if(empty($param['guidt'])){
                return outputJson(4000, '请输入归档日期');
            }


            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $jiaoid = $request->param('jiaoid');
            $dangtype = $request->param('dangtype');
            unset($param['jiaoid']);
            unset($param['dangtype']);

            Db::startTrans();
            try {

                if($jiaoid){
                    if($dangtype == 1){
                        DossJiaoModel::where(['id'=>$jiaoid])->update(['isjie'=>1]);
                    }elseif($dangtype == 2){
                        CossJiaoModel::where(['id'=>$jiaoid])->update(['isjie'=>1]);
                    }elseif($dangtype == 3){
                        XossJiaoModel::where(['id'=>$jiaoid])->update(['isjie'=>1]);
                    }
                }


                $id = CossModel::insertGetId($param);

                (new EnginRelevan())->vate_coss_number($id);


                (new OptService())->relation_file($files,'coss',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'coss', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'coss', $id, '财务档案库');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $dangtype = $request->get('dangtype',0,'int');
            $jiaoid = $request->get('jiaoid',0,'int');

            $fileArr = [];
            if($dangtype == 1){
                $data['jiaoinfo'] = DossJiaoModel::where(['id'=>$jiaoid])->find();
                if($data['jiaoinfo']){
                    $data['jiaoinfo']['engininfo'] = (new EnginService())->get_engin_showinfo($data['jiaoinfo']['enginid']);
                }
                $fileArr = FileModel::where(['table'=>'doss','mid'=>$data['jiaoinfo']['yiid'],'comid'=>$this->userinfo['comid']])->select()->toArray();
            }elseif($dangtype == 2){
                $data['jiaoinfo'] = CossJiaoModel::where(['id'=>$jiaoid])->find();
                if($data['jiaoinfo']){
                    $data['jiaoinfo']['engininfo'] = (new EnginService())->get_engin_showinfo($data['jiaoinfo']['enginid']);
                }
                $fileArr = FileModel::where(['table'=>'coss','mid'=>$data['jiaoinfo']['yiid'],'comid'=>$this->userinfo['comid']])->select()->toArray();
            }elseif($dangtype == 3){
                $data['jiaoinfo'] = XossJiaoModel::where(['id'=>$jiaoid])->find();
                if($data['jiaoinfo']){
                    $data['jiaoinfo']['engininfo'] = (new EnginService())->get_engin_showinfo($data['jiaoinfo']['enginid']);
                }
                $fileArr = FileModel::where(['table'=>'xoss','mid'=>$data['jiaoinfo']['yiid'],'comid'=>$this->userinfo['comid']])->select()->toArray();
            }
            FileModel::where(['table'=>'coss','mid'=>-$this->userinfo['id'],'comid'=>$this->userinfo['comid']])->delete();
            $typeid = FiletypeModel::where(['comid'=>$this->userinfo['comid'],'table'=>'coss','name'=>'相关附件'])->value('id');
            foreach ($fileArr as $fff){
                $ins = $fff;
                unset($ins['id']);
                $ins['table'] = 'coss';
                $ins['typeid'] = $typeid;
                $ins['mid'] = -$this->userinfo['id'];
                $ins['created_user'] = $this->userinfo['id'];
                $ins['created_at'] = date("Y-m-d H:i:s");
                $ins_id = FileModel::insertGetId($ins);
                $encpath =  encrypt($ins_id, 'E', Config::get('service.info_key'));
                if(FileModel::where(['id'=>$ins_id])->update(['encpath'=>$encpath]) === false){
                    return outputJson(4000,"附件处理失败,请重新操作");
                }
            }
            $data['files'] = (new OptService())->get_file('coss', -$this->userinfo['id']);

            $enginid = $request->get('enginid',0,'int');
            $data['engininfo'] = (new EnginService())->get_engin_showinfo($enginid);

            $data['guier_info'] = (new UserService())->get_users_part_arr($this->userinfo['id'], 'user');
            $data['dangfen'] = (new OptService())->get_base_info('coss_dangfen');
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_coss(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['dangfen'])){
                return outputJson(4000, '请选择档案分类');
            }
            if(empty($param['name'])){
                return outputJson(4000, '请输入档案名称');
            }
            if(empty($param['danglei'])){
                return outputJson(4000, '请选择档案类型');
            }
            if(empty($param['guinum'])){
                return outputJson(4000, '请输入归档份数');
            }
            if(empty($param['guidt'])){
                return outputJson(4000, '请输入归档日期');
            }
            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                CossModel::update($param);
                (new OptService())->relation_file($files,'coss',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'coss', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'coss', $param['id'], '财务档案库');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = CossModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }
            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
            $data['data']['guier_info'] = (new UserService())->get_users_part_arr($data['data']['guier'], 'user');
            $data['dangfen'] = (new OptService())->get_base_info('coss_dangfen');
            $data['files'] = (new OptService())->get_file('coss', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function jiao_coss(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['yier'])){
                return outputJson(4000, '请选择移交人');
            }
            if(empty($param['yidt'])){
                return outputJson(4000, '请选择移交日期');
            }
            if(empty($param['yinum'])){
                return outputJson(4000, '请输入移交份数');
            }
            if(empty($param['jieer'])){
                return outputJson(4000, '请选择通知人');
            }


            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            unset($param['token']);

            $danginfo = CossModel::where(['id'=>$param['yiid']])->find();
            $param['jiaoqian'] = 2;
            $param['enginid'] = $danginfo['enginid'];
            $param['number'] = $danginfo['number'];
            $param['dangfen'] = $danginfo['dangfen'];
            $param['dangfen_name'] = (new OptService())->get_base_showname('coss_dangfen', $danginfo['dangfen']);
            $param['name'] = $danginfo['name'];
            $param['dangdt'] = $danginfo['dangdt'];
            $param['xianger'] = $danginfo['xianger'];
            $param['shefee'] = $danginfo['shefee'];
            $param['yeshu'] = $danginfo['yeshu'];
            $param['runum'] = $danginfo['runum'];
            $param['guinum'] = $danginfo['guinum'];
            $param['guidt'] = $danginfo['guidt'];
            $param['guier'] = $danginfo['guier'];
            $param['explain'] = $danginfo['explain'];

            Db::startTrans();
            try {
                $id = CossJiaoModel::insertGetId($param);

                //档案移交提醒消息：[来源]移交档案[档案名称]至[移交至]，请及时接收。
                $jiaohouName = "";
                if($param['jiaohou'] == 1){
                    $jiaohouName = '工程档案库';
                }elseif($param['jiaohou'] == 2){
                    $jiaohouName = '财务档案库';
                }elseif($param['jiaohou'] == 3){
                    $jiaohouName = '行政档案库';
                }
                $mess = "[财务档案库]移交档案[".$param['name']."]至[".$jiaohouName."]，请及时接收。";
                (new OptService())->add_todo($param['jieer'], '档案移交',$mess, $id, 'coss_jiao');

                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'coss_jiao', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'coss_jiao', $id, '财务档案移交');

                (new OptService())->add_flow_log('移交', 'coss', $param['yiid']);
                (new OptService())->add_log_list('移交', 'coss', $param['yiid'], '财务档案库');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = CossModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }
            $data['data']['yidt'] = date("Y-m-d");
            $data['data']['yier_info'] = (new UserService())->get_users_part_arr($this->userinfo['id'], 'user');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function fei_coss(Request $request){
        if($request->isPost()){
            $param = $request->post();


            if(empty($param['feier'])){
                return outputJson(4000, '请选择作废人');
            }
            if(empty($param['feidt'])){
                return outputJson(4000, '请选择作废日期');
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            unset($param['token']);

            if(isset($param['tongtype'])){
                $tongtype = $param['tongtype'];
                unset($param['tongtype']);

                foreach ($tongtype as $vvv){
                    if($vvv == 1){$param['fei_gong'] = 1;}
                    if($vvv == 2){$param['fei_cai'] = 1;}
                    if($vvv == 3){$param['fei_xing'] = 1;}
                }
            }

            $param['isfei'] = 1;


            Db::startTrans();
            try {
                CossModel::update($param);
                (new OptService())->add_flow_log('作废', 'coss', $param['id']);
                (new OptService())->add_log_list('作废', 'coss', $param['id'], '工程档案库');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = CossModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }
            $data['data']['feidt'] = date("Y-m-d");
            $data['data']['feier_info'] = (new UserService())->get_users_part_arr($this->userinfo['id'], 'user');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function qufei_coss(Request $request){
        $param = $request->post();
        $data = CossModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            CossModel::where(['id'=>$param['id']])->update([
                'isfei'=>0,
                'fei_gong'=>0,
                'fei_cai'=>0,
                'fei_xing'=>0,
                'feier'=>0,
                'feidt'=>'',
                'feishuo'=>'',
            ]);

            (new OptService())->add_flow_log('取消作废', 'coss', $param['id']);
            (new OptService())->add_log_list('取消作废', 'coss', $param['id'], '财务档案库');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function del_coss(Request $request){
        $param = $request->post();
        $data = CossModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            CossModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);

            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'coss', $data['id'], '财务档案库');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_coss(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = CossModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }
        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
        $data['data']['guier_name'] = UserModel::get_realname_by_pk($data['data']['guier']);
        $data['data']['dangfen_name'] = (new OptService())->get_base_showname('coss_dangfen', $data['data']['dangfen']);
        $data['files'] = (new OptService())->get_file('coss', $data['data']['id']);
        return outputJson(2000,lang('code_2000'), $data);
    }
    public function down_coss(Request $request){
        $id = $request->param('id', 0, 'int');
        $data['data'] = CossModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }


        $id_arr = FileModel::where(['table'=>'coss','mid'=>$data['data']['id']])->column('id');
        $filename = $data['data']['name'];

        if(!$id_arr){
            return outputJson(4000, '暂无附件下载');
        }

        Db::startTrans();
        try {
            $file_dir = 'tmp_zip/';
            $filename = $filename.'.zip';

            if(file_exists($file_dir.$filename)){
                unlink($file_dir.$filename);
            }

            $zip = new \ZipArchive();
            $res = $zip->open($file_dir.$filename, $zip::CREATE);
            if ($res === TRUE) {
                foreach ($id_arr as $fileid) {
                    if($fileid < 1){
                        continue;
                    }
                    $_file = FileModel::where(['id'=>$fileid])->find();
                    if(!$_file || $_file['comid'] != $this->userinfo['comid']){
                        throw new Exception("操作失败");
                    }

                    $zip->addFile($_file['oripath'], substr($_file['oripath'],22));
                }
            }
            $zip->close();

        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'), $file_dir.$filename);
    }

    public function list_coss_jiao(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['dang.deleted_user','=', 0];
        $where[] = ['dang.comid','=',$this->userinfo['comid']];
        $where[] = ['dang.jiaohou','=',2];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|dang.number|dang.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['dang.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['dang.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['isjie']) && $param['isjie'] != ''){
            $where[] = ['dang.isjie','=', $param['isjie']];
        }

        $list = [];

        $dossinfo = DossJiaoModel::alias('dang')
            ->field('dang.*,e.name as enginname')
            ->leftJoin('engin e','e.id=dang.enginid')
            ->where($where)
            ->select()->toArray();
        foreach($dossinfo as $dossvv){
            $dossvv['dangtype'] = 1;
            $dossvv['yier_name'] = UserModel::get_realname_by_pk($dossvv['yier']);
            $dossvv['jieer_name'] = UserModel::get_realname_by_pk($dossvv['jieer']);
            $list[] = $dossvv;
        }


        $xossinfo = XossJiaoModel::alias('dang')
            ->field('dang.*,e.name as enginname')
            ->leftJoin('engin e','e.id=dang.enginid')
            ->where($where)
            ->select()->toArray();
        foreach($xossinfo as $xossvv){
            $xossvv['dangtype'] = 3;
            $xossvv['yier_name'] = UserModel::get_realname_by_pk($xossvv['yier']);
            $xossvv['jieer_name'] = UserModel::get_realname_by_pk($xossvv['jieer']);
            $list[] = $xossvv;
        }


        $dtSort = array_column($list,'yidt');
        array_multisort($dtSort,SORT_DESC, $list);
        $data['data'] = $list;

        $data['butauth'] = (new MenuService())->get_butauth('coss_jiao');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_coss_jiao(Request $request){
        $param = $request->post();
        $data = CossJiaoModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            CossJiaoModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'coss_jiao', $data['id'], '待接收档案');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_coss_jiao(Request $request){
        $dangtype = $request->post('dangtype', 0, 'int');
        $id = $request->post('id', 0, 'int');

        if($dangtype == 1){
            $data['data'] = DossJiaoModel::get_info_by_pk($id);
            $data['files'] = (new OptService())->get_file('doss', $data['data']['yiid']);
        }elseif($dangtype == 2){
            $data['data'] = CossJiaoModel::get_info_by_pk($id);
            $data['files'] = (new OptService())->get_file('coss', $data['data']['yiid']);
        }elseif($dangtype == 3){
            $data['data'] = XossJiaoModel::get_info_by_pk($id);
            $data['files'] = (new OptService())->get_file('xoss', $data['data']['yiid']);
        }


        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }
        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);

        $data['data']['yier_name'] = UserModel::get_realname_by_pk($data['data']['yier']);
        $data['data']['jieer_name'] = UserModel::get_realname_by_pk($data['data']['jieer']);
        return outputJson(2000,lang('code_2000'), $data);
    }
    public function list_coss_fei(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['dang.deleted_user','=', 0];
        $where[] = ['dang.comid','=',$this->userinfo['comid']];
        $where[] = ['dang.isfei','=',1];
        $where[] = ['dang.fei_cai','=',1];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|dang.number|dang.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['dang.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['dang.name','like', '%'.$param['name'].'%'];
        }

        $list = [];

        $dossinfo = DossModel::alias('dang')
            ->field('dang.*,e.name as enginname')
            ->leftJoin('engin e','e.id=dang.enginid')
            ->where($where)
            ->select()->toArray();
        foreach($dossinfo as $dossvv){
            $dossvv['dangtype'] = 1;
            $dossvv['dangfen_name'] = (new OptService())->get_base_showname('doss_dangfen', $dossvv['dangfen']);
            $list[] = $dossvv;
        }


        $xossinfo = XossModel::alias('dang')
            ->field('dang.*,e.name as enginname')
            ->leftJoin('engin e','e.id=dang.enginid')
            ->where($where)
            ->select()->toArray();
        foreach($xossinfo as $xossvv){
            $xossvv['dangtype'] = 3;
            $xossvv['dangfen_name'] = (new OptService())->get_base_showname('xoss_dangfen', $xossvv['dangfen']);
            $list[] = $xossvv;
        }


        $dtSort = array_column($list,'feidt');
        array_multisort($dtSort,SORT_DESC, $list);
        $data['data'] = $list;

        $data['butauth'] = (new MenuService())->get_butauth('doss_fei');
        return outputJson(2000,lang('code_2000'),$data);
    }

    public function detail_coss_jie(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['jie.deleted_user','=', 0];
        $where[] = ['jie.comid','=',$this->userinfo['comid']];
        $where[] = ['jie.isturn','=',1];
        $where[] = ['jie.status','in',[0,1]];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['bill.sericnum|jieer.realname|coss.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['sericnum']) && !empty($param['sericnum'])){
            $where[] = ['bill.sericnum','like', '%'.$param['sericnum'].'%'];
        }
        if(isset($param['jieername']) && !empty($param['jieername'])){
            $where[] = ['jieer.realname','like', '%'.$param['jieername'].'%'];
        }
        if(isset($param['coss_name']) && !empty($param['coss_name'])){
            $where[] = ['coss.name','like', '%'.$param['coss_name'].'%'];
        }


        $data['data'] = CossJieListModel::alias('list')
            ->field('jie.*,jieer.realname as jieername,dept.name as jieerdept,
            list.id as listid,list.explain as list_explain,list.guidt,list.guier,list.state,list.num,guier.realname as guiername,
            coss.name as coss_name,coss.number as coss_number,coss.dangfen,e.name as enginname,bill.sericnum')
            ->join('coss_jie jie','jie.id=list.jieid')
            ->leftJoin('flow_bill bill','bill.id=jie.billid')
            ->leftJoin('coss coss','coss.id=list.cossid')
            ->leftJoin('engin e','e.id=coss.enginid')
            ->leftJoin('user jieer','jieer.id=jie.jieer')
            ->leftJoin('dept dept','dept.id=jieer.deptid')
            ->leftJoin('user guier','guier.id=list.guier')
            ->where($where)
            ->where((new MenuService())->get_miss_where('coss_jie', 'jie'))
            ->order('jie.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['dangfen_name'] = (new OptService())->get_base_showname('coss_dangfen', $item['dangfen']);
                $item['gui_state_name'] = $item['state'] > 0 ? '已归还' : '未归还';
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('coss_jie_detail');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_coss_jie(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['jie.deleted_user','=', 0];
        $where[] = ['jie.comid','=',$this->userinfo['comid']];
        $where[] = ['jie.isturn','=',1];
        $where[] = ['jie.status','in',[0,1]];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['jieer.realname|jie.yong','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['jieername']) && !empty($param['jieername'])){
            $where[] = ['jieer.realname','like', '%'.$param['jieername'].'%'];
        }
        if(isset($param['yong']) && !empty($param['yong'])){
            $where[] = ['jie.yong','like', '%'.$param['yong'].'%'];
        }
        if(isset($param['yudt']) && !empty($param['yudt'])){
            $where[] = ['jie.yudt','between', explode(' - ', $param['yudt'])];
        }
        if(isset($param['state']) && $param['state'] != ''){
            $where[] = ['jie.state','=', $param['state']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['jie.status','=', $param['status']];
        }


        $data['data'] = CossJieModel::alias('jie')
            ->field('jie.*,jieer.realname as jieername,dept.name as deptname')
            ->leftJoin('user jieer','jieer.id=jie.jieer')
            ->leftJoin('dept dept','dept.id=jieer.deptid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('coss_jie', 'jie'))
            ->order('jie.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $listOneCossid = CossJieListModel::where(['jieid'=>$item['id']])->value('cossid');
                $item['jietuname'] = CossModel::where(['id'=>$listOneCossid])->value('name');
                $listNum = CossJieListModel::where(['jieid'=>$item['id']])->count();
                if($listNum > 1){
                    $item['jietuname'] .= '...等'.$listNum.'本';
                }
                $item['state_name'] = (new XingService())->get_coss_jie_state_name($item['state']);

                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('coss_jie');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_coss_jie(Request $request){
        $param = $request->post();
        $data = CossJieModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            CossJieModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'coss_jie', $data['id'], '财务档案借阅');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_coss_jie(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = CossJieModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $jieerinfo = UserModel::where(['id'=>$data['data']['jieer']])->find();
        $data['data']['jieer_name'] = $jieerinfo['realname'];
        $data['data']['dept_name'] = DeptModel::get_name_by_pk($jieerinfo['deptid']);

        $list = CossJieListModel::where(['jieid'=>$data['data']['id']])->select()->toArray();
        foreach ($list as &$item){
            $cossinfo = CossModel::where(['id'=>$item['cossid']])->find();
            if($cossinfo){
                $item['dangfen_name'] = (new OptService())->get_base_showname('coss_dangfen', $cossinfo['dangfen']);

                $item['name'] = $cossinfo['name'];
                $item['number'] = $cossinfo['number'];
                $item['enginname'] = EnginModel::where(['id'=>$cossinfo['enginid']])->value('name');
            }
            $item['guier_name'] = UserModel::get_realname_by_pk($item['guier']);
        }
        $data['list'] = $list;

        $data['files'] = (new OptService())->get_file('coss_jie', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }
    public function gui_coss_jie(Request $request){
        if($request->isPost()){
            $param = $request->post();


            if(empty($param['guidt'])){
                return outputJson(4000, '请选择归还日期');
            }
            if(empty($param['guier'])){
                return outputJson(4000, '请选择接收人');
            }

            $param['state'] = 1;
            unset($param['token']);

            $listinfo = CossJieListModel::where(['id'=>$param['id']])->find();



            Db::startTrans();
            try {
                CossJieListModel::update($param);

                $jie_state = 1;
                $nohuan = CossJieListModel::where(['jieid'=>$listinfo['jieid'],'state'=>0])->find();
                if(!$nohuan){
                    $jie_state = 2;
                }
                CossJieModel::where(['id'=>$listinfo['jieid']])->update(['state'=>$jie_state]);


                (new OptService())->add_flow_log('财务档案归还', 'coss_jie', $listinfo['id']);
                (new OptService())->add_log_list('财务档案归还', 'coss_jie', $listinfo['id'], '财务档案借阅');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = CossJieListModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }
            $data['cossinfo'] = CossModel::where(['id'=>$data['data']['cossid']])->find();

            if($data['data']['state'] == 0){
                $data['data']['guidt'] = date("Y-m-d");
                $data['data']['guier_info'] = (new UserService())->get_users_part_arr($this->userinfo['id'], 'user');
            }else{
                $data['data']['guier_info'] = (new UserService())->get_users_part_arr($data['data']['guier'], 'user');
            }
            return outputJson(2000,lang('code_2000'),$data);
        }
    }









    public function list_xoss(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['xoss.deleted_user','=', 0];
        $where[] = ['xoss.comid','=',$this->userinfo['comid']];

        if(isset($param['isfei']) && $param['isfei'] != ''){
            $where[] = ['xoss.isfei','=', $param['isfei']];
        }else{
            $where[] = ['xoss.isfei','=',0];
        }


        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['xoss.enginid','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|xoss.number|xoss.name|xoss.xianger|xoss.danglei|guier.realname','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['xoss.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['xoss.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['xianger']) && !empty($param['xianger'])){
            $where[] = ['xoss.xianger','like', '%'.$param['xianger'].'%'];
        }
        if(isset($param['danglei']) && !empty($param['danglei'])){
            $where[] = ['xoss.danglei','like', '%'.$param['danglei'].'%'];
        }
        if(isset($param['guiername']) && !empty($param['guiername'])){
            $where[] = ['guier.realname','like', '%'.$param['guiername'].'%'];
        }
        if(isset($param['dangdt']) && !empty($param['dangdt'])){
            $where[] = ['xoss.dangdt','between', explode(' - ', $param['dangdt'])];
        }
        if(isset($param['guidt']) && !empty($param['guidt'])){
            $where[] = ['xoss.guidt','between', explode(' - ', $param['guidt'])];
        }
        if(isset($param['dangfen']) && !empty($param['dangfen'])){
            $where[] = ['xoss.dangfen','=', $param['dangfen']];
        }

        $data['dangfen'] = (new OptService())->get_base_info('xoss_dangfen');
        $data['is_dangfen'] = isset($param['dangfen']) ? $param['dangfen'] : '';

        $data['data'] = XossModel::alias('xoss')
            ->field('xoss.*,guier.realname as guiername,e.name as enginname')
            ->join('user guier','guier.id=xoss.guier')
            ->leftJoin('engin e','e.id=xoss.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('xoss', 'xoss'))
            ->order('xoss.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['dangfen_name'] = (new OptService())->get_base_showname('xoss_dangfen', $item['dangfen']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('xoss');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function yiinfo_xoss(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['xoss.deleted_user','=', 0];
        $where[] = ['xoss.comid','=',$this->userinfo['comid']];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['xoss.enginid','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|xoss.number|xoss.name|xoss.xianger|xoss.danglei|guier.realname','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['xoss.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['xoss.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['xianger']) && !empty($param['xianger'])){
            $where[] = ['xoss.xianger','like', '%'.$param['xianger'].'%'];
        }
        if(isset($param['danglei']) && !empty($param['danglei'])){
            $where[] = ['xoss.danglei','like', '%'.$param['danglei'].'%'];
        }
        if(isset($param['guiername']) && !empty($param['guiername'])){
            $where[] = ['guier.realname','like', '%'.$param['guiername'].'%'];
        }
        if(isset($param['dangdt']) && !empty($param['dangdt'])){
            $where[] = ['xoss.dangdt','between', explode(' - ', $param['dangdt'])];
        }
        if(isset($param['guidt']) && !empty($param['guidt'])){
            $where[] = ['xoss.guidt','between', explode(' - ', $param['guidt'])];
        }
        if(isset($param['dangfen']) && !empty($param['dangfen'])){
            $where[] = ['xoss.dangfen','=', $param['dangfen']];
        }

        $data['dangfen'] = (new OptService())->get_base_info('xoss_dangfen');
        $data['is_dangfen'] = isset($param['dangfen']) ? $param['dangfen'] : '';

        $data['data'] = XossJiaoModel::alias('xoss')
            ->field('xoss.*,guier.realname as guiername,e.name as enginname')
            ->join('user guier','guier.id=xoss.guier')
            ->leftJoin('engin e','e.id=xoss.enginid')
            ->where($where)
            ->order('xoss.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_xoss(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['dangfen'])){
                return outputJson(4000, '请选择档案分类');
            }
            if(empty($param['name'])){
                return outputJson(4000, '请输入档案名称');
            }
            if(empty($param['danglei'])){
                return outputJson(4000, '请选择档案类型');
            }
            if(empty($param['guinum'])){
                return outputJson(4000, '请输入归档份数');
            }
            if(empty($param['guidt'])){
                return outputJson(4000, '请输入归档日期');
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $jiaoid = $request->param('jiaoid');
            $dangtype = $request->param('dangtype');
            unset($param['jiaoid']);
            unset($param['dangtype']);

            Db::startTrans();
            try {

                if($jiaoid){
                    if($dangtype == 1){
                        DossJiaoModel::where(['id'=>$jiaoid])->update(['isjie'=>1]);
                    }elseif($dangtype == 2){
                        CossJiaoModel::where(['id'=>$jiaoid])->update(['isjie'=>1]);
                    }elseif($dangtype == 3){
                        XossJiaoModel::where(['id'=>$jiaoid])->update(['isjie'=>1]);
                    }
                }


                $id = XossModel::insertGetId($param);

                (new EnginRelevan())->vate_xoss_number($id);

                (new OptService())->relation_file($files,'xoss',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'xoss', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'xoss', $id, '行政档案库');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $dangtype = $request->get('dangtype',0,'int');
            $jiaoid = $request->get('jiaoid',0,'int');

            $fileArr = [];
            if($dangtype == 1){
                $data['jiaoinfo'] = DossJiaoModel::where(['id'=>$jiaoid])->find();
                if($data['jiaoinfo']){
                    $data['jiaoinfo']['engininfo'] = (new EnginService())->get_engin_showinfo($data['jiaoinfo']['enginid']);
                }
                $fileArr = FileModel::where(['table'=>'doss','mid'=>$data['jiaoinfo']['yiid'],'comid'=>$this->userinfo['comid']])->select()->toArray();
            }elseif($dangtype == 2){
                $data['jiaoinfo'] = CossJiaoModel::where(['id'=>$jiaoid])->find();
                if($data['jiaoinfo']){
                    $data['jiaoinfo']['engininfo'] = (new EnginService())->get_engin_showinfo($data['jiaoinfo']['enginid']);
                }
                $fileArr = FileModel::where(['table'=>'coss','mid'=>$data['jiaoinfo']['yiid'],'comid'=>$this->userinfo['comid']])->select()->toArray();
            }elseif($dangtype == 3){
                $data['jiaoinfo'] = XossJiaoModel::where(['id'=>$jiaoid])->find();
                if($data['jiaoinfo']){
                    $data['jiaoinfo']['engininfo'] = (new EnginService())->get_engin_showinfo($data['jiaoinfo']['enginid']);
                }
                $fileArr = FileModel::where(['table'=>'xoss','mid'=>$data['jiaoinfo']['yiid'],'comid'=>$this->userinfo['comid']])->select()->toArray();
            }
            FileModel::where(['table'=>'xoss','mid'=>-$this->userinfo['id'],'comid'=>$this->userinfo['comid']])->delete();
            $typeid = FiletypeModel::where(['comid'=>$this->userinfo['comid'],'table'=>'xoss','name'=>'相关附件'])->value('id');
            foreach ($fileArr as $fff){
                $ins = $fff;
                unset($ins['id']);
                $ins['table'] = 'xoss';
                $ins['typeid'] = $typeid;
                $ins['mid'] = -$this->userinfo['id'];
                $ins['created_user'] = $this->userinfo['id'];
                $ins['created_at'] = date("Y-m-d H:i:s");
                $ins_id = FileModel::insertGetId($ins);
                $encpath =  encrypt($ins_id, 'E', Config::get('service.info_key'));
                if(FileModel::where(['id'=>$ins_id])->update(['encpath'=>$encpath]) === false){
                    return outputJson(4000,"附件处理失败,请重新操作");
                }
            }
            $data['files'] = (new OptService())->get_file('xoss', -$this->userinfo['id']);


            $enginid = $request->get('enginid',0,'int');
            $data['engininfo'] = (new EnginService())->get_engin_showinfo($enginid);


            $data['guier_info'] = (new UserService())->get_users_part_arr($this->userinfo['id'], 'user');
            $data['dangfen'] = (new OptService())->get_base_info('xoss_dangfen');
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_xoss(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['dangfen'])){
                return outputJson(4000, '请选择档案分类');
            }
            if(empty($param['name'])){
                return outputJson(4000, '请输入档案名称');
            }
            if(empty($param['danglei'])){
                return outputJson(4000, '请选择档案类型');
            }
            if(empty($param['guinum'])){
                return outputJson(4000, '请输入归档份数');
            }
            if(empty($param['guidt'])){
                return outputJson(4000, '请输入归档日期');
            }
            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                XossModel::update($param);
                (new OptService())->relation_file($files,'xoss',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'xoss', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'xoss', $param['id'], '行政档案库');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = XossModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }
            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
            $data['data']['guier_info'] = (new UserService())->get_users_part_arr($data['data']['guier'], 'user');
            $data['dangfen'] = (new OptService())->get_base_info('xoss_dangfen');
            $data['files'] = (new OptService())->get_file('xoss', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function jiao_xoss(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['yier'])){
                return outputJson(4000, '请选择移交人');
            }
            if(empty($param['yidt'])){
                return outputJson(4000, '请选择移交日期');
            }
            if(empty($param['yinum'])){
                return outputJson(4000, '请输入移交份数');
            }
            if(empty($param['jieer'])){
                return outputJson(4000, '请选择通知人');
            }


            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            unset($param['token']);

            $danginfo = XossModel::where(['id'=>$param['yiid']])->find();
            $param['jiaoqian'] = 3;
            $param['enginid'] = $danginfo['enginid'];
            $param['number'] = $danginfo['number'];
            $param['dangfen'] = $danginfo['dangfen'];
            $param['dangfen_name'] = (new OptService())->get_base_showname('xoss_dangfen', $danginfo['dangfen']);
            $param['name'] = $danginfo['name'];
            $param['dangdt'] = $danginfo['dangdt'];
            $param['xianger'] = $danginfo['xianger'];
            $param['shefee'] = $danginfo['shefee'];
            $param['yeshu'] = $danginfo['yeshu'];
            $param['runum'] = $danginfo['runum'];
            $param['guinum'] = $danginfo['guinum'];
            $param['guidt'] = $danginfo['guidt'];
            $param['guier'] = $danginfo['guier'];
            $param['explain'] = $danginfo['explain'];

            Db::startTrans();
            try {
                $id = XossJiaoModel::insertGetId($param);

                //档案移交提醒消息：[来源]移交档案[档案名称]至[移交至]，请及时接收。
                $jiaohouName = "";
                if($param['jiaohou'] == 1){
                    $jiaohouName = '工程档案库';
                }elseif($param['jiaohou'] == 2){
                    $jiaohouName = '财务档案库';
                }elseif($param['jiaohou'] == 3){
                    $jiaohouName = '行政档案库';
                }
                $mess = "[行政档案库]移交档案[".$param['name']."]至[".$jiaohouName."]，请及时接收。";
                (new OptService())->add_todo($param['jieer'], '档案移交',$mess, $id, 'xoss_jiao');

                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'xoss_jiao', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'xoss_jiao', $id, '行政档案移交');

                (new OptService())->add_flow_log('移交', 'xoss', $param['yiid']);
                (new OptService())->add_log_list('移交', 'xoss', $param['yiid'], '行政档案库');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = XossModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }
            $data['data']['yidt'] = date("Y-m-d");
            $data['data']['yier_info'] = (new UserService())->get_users_part_arr($this->userinfo['id'], 'user');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function fei_xoss(Request $request){
        if($request->isPost()){
            $param = $request->post();


            if(empty($param['feier'])){
                return outputJson(4000, '请选择作废人');
            }
            if(empty($param['feidt'])){
                return outputJson(4000, '请选择作废日期');
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            unset($param['token']);

            if(isset($param['tongtype'])){
                $tongtype = $param['tongtype'];
                unset($param['tongtype']);

                foreach ($tongtype as $vvv){
                    if($vvv == 1){$param['fei_gong'] = 1;}
                    if($vvv == 2){$param['fei_cai'] = 1;}
                    if($vvv == 3){$param['fei_xing'] = 1;}
                }
            }

            $param['isfei'] = 1;


            Db::startTrans();
            try {
                XossModel::update($param);
                (new OptService())->add_flow_log('作废', 'xoss', $param['id']);
                (new OptService())->add_log_list('作废', 'xoss', $param['id'], '行政档案库');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = XossModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }
            $data['data']['feidt'] = date("Y-m-d");
            $data['data']['feier_info'] = (new UserService())->get_users_part_arr($this->userinfo['id'], 'user');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function qufei_xoss(Request $request){
        $param = $request->post();
        $data = XossModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            XossModel::where(['id'=>$param['id']])->update([
                'isfei'=>0,
                'fei_gong'=>0,
                'fei_cai'=>0,
                'fei_xing'=>0,
                'feier'=>0,
                'feidt'=>'',
                'feishuo'=>'',
            ]);

            (new OptService())->add_flow_log('取消作废', 'xoss', $param['id']);
            (new OptService())->add_log_list('取消作废', 'xoss', $param['id'], '行政档案库');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function del_xoss(Request $request){
        $param = $request->post();
        $data = XossModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            XossModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);

            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'xoss', $data['id'], '行政档案库');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_xoss(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = XossModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }
        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
        $data['data']['guier_name'] = UserModel::get_realname_by_pk($data['data']['guier']);
        $data['data']['dangfen_name'] = (new OptService())->get_base_showname('xoss_dangfen', $data['data']['dangfen']);
        $data['files'] = (new OptService())->get_file('xoss', $data['data']['id']);
        return outputJson(2000,lang('code_2000'), $data);
    }
    public function down_xoss(Request $request){
        $id = $request->param('id', 0, 'int');
        $data['data'] = XossModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }


        $id_arr = FileModel::where(['table'=>'xoss','mid'=>$data['data']['id']])->column('id');
        $filename = $data['data']['name'];

        if(!$id_arr){
            return outputJson(4000, '暂无附件下载');
        }

        Db::startTrans();
        try {
            $file_dir = 'tmp_zip/';
            $filename = $filename.'.zip';

            if(file_exists($file_dir.$filename)){
                unlink($file_dir.$filename);
            }

            $zip = new \ZipArchive();
            $res = $zip->open($file_dir.$filename, $zip::CREATE);
            if ($res === TRUE) {
                foreach ($id_arr as $fileid) {
                    if($fileid < 1){
                        continue;
                    }
                    $_file = FileModel::where(['id'=>$fileid])->find();
                    if(!$_file || $_file['comid'] != $this->userinfo['comid']){
                        throw new Exception("操作失败");
                    }

                    $zip->addFile($_file['oripath'], substr($_file['oripath'],22));
                }
            }
            $zip->close();

        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'), $file_dir.$filename);
    }

    public function list_xoss_jiao(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['dang.deleted_user','=', 0];
        $where[] = ['dang.comid','=',$this->userinfo['comid']];
        $where[] = ['dang.jiaohou','=',3];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|dang.number|dang.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['dang.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['dang.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['isjie']) && $param['isjie'] != ''){
            $where[] = ['dang.isjie','=', $param['isjie']];
        }

        $list = [];

        $dossinfo = dossJiaoModel::alias('dang')
            ->field('dang.*,e.name as enginname')
            ->leftJoin('engin e','e.id=dang.enginid')
            ->where($where)
            ->select()->toArray();
        foreach($dossinfo as $dossvv){
            $dossvv['dangtype'] = 1;
            $dossvv['yier_name'] = UserModel::get_realname_by_pk($dossvv['yier']);
            $dossvv['jieer_name'] = UserModel::get_realname_by_pk($dossvv['jieer']);
            $list[] = $dossvv;
        }

        $cossinfo = CossJiaoModel::alias('dang')
            ->field('dang.*,e.name as enginname')
            ->leftJoin('engin e','e.id=dang.enginid')
            ->where($where)
            ->select()->toArray();
        foreach($cossinfo as $cossvv){
            $cossvv['dangtype'] = 2;
            $cossvv['yier_name'] = UserModel::get_realname_by_pk($cossvv['yier']);
            $cossvv['jieer_name'] = UserModel::get_realname_by_pk($cossvv['jieer']);
            $list[] = $cossvv;
        }



        $dtSort = array_column($list,'yidt');
        array_multisort($dtSort,SORT_DESC, $list);
        $data['data'] = $list;

        $data['butauth'] = (new MenuService())->get_butauth('xoss_jiao');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_xoss_jiao(Request $request){
        $param = $request->post();
        $data = XossJiaoModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            XossJiaoModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'xoss_jiao', $data['id'], '待接收档案');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_xoss_jiao(Request $request){
        $dangtype = $request->post('dangtype', 0, 'int');
        $id = $request->post('id', 0, 'int');
        if($dangtype == 1){
            $data['data'] = DossJiaoModel::get_info_by_pk($id);
            $data['files'] = (new OptService())->get_file('doss', $data['data']['yiid']);
        }elseif($dangtype == 2){
            $data['data'] = CossJiaoModel::get_info_by_pk($id);
            $data['files'] = (new OptService())->get_file('coss', $data['data']['yiid']);
        }elseif($dangtype == 3){
            $data['data'] = XossJiaoModel::get_info_by_pk($id);
            $data['files'] = (new OptService())->get_file('xoss', $data['data']['yiid']);
        }
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }
        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);

        $data['data']['yier_name'] = UserModel::get_realname_by_pk($data['data']['yier']);
        $data['data']['jieer_name'] = UserModel::get_realname_by_pk($data['data']['jieer']);
        return outputJson(2000,lang('code_2000'), $data);
    }
    public function list_xoss_fei(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['dang.deleted_user','=', 0];
        $where[] = ['dang.comid','=',$this->userinfo['comid']];
        $where[] = ['dang.isfei','=',1];
        $where[] = ['dang.fei_xing','=',1];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|dang.number|dang.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['dang.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['dang.name','like', '%'.$param['name'].'%'];
        }

        $list = [];

        $dossinfo = DossModel::alias('dang')
            ->field('dang.*,e.name as enginname')
            ->leftJoin('engin e','e.id=dang.enginid')
            ->where($where)
            ->select()->toArray();
        foreach($dossinfo as $dossvv){
            $dossvv['dangtype'] = 1;
            $dossvv['dangfen_name'] = (new OptService())->get_base_showname('doss_dangfen', $dossvv['dangfen']);
            $list[] = $dossvv;
        }


        $cossinfo = CossModel::alias('dang')
            ->field('dang.*,e.name as enginname')
            ->leftJoin('engin e','e.id=dang.enginid')
            ->where($where)
            ->select()->toArray();
        foreach($cossinfo as $cossvv){
            $cossvv['dangtype'] = 2;
            $cossvv['dangfen_name'] = (new OptService())->get_base_showname('coss_dangfen', $cossvv['dangfen']);
            $list[] = $cossvv;
        }


        $dtSort = array_column($list,'feidt');
        array_multisort($dtSort,SORT_DESC, $list);
        $data['data'] = $list;

        $data['butauth'] = (new MenuService())->get_butauth('doss_fei');
        return outputJson(2000,lang('code_2000'),$data);
    }


    public function detail_xoss_jie(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['jie.deleted_user','=', 0];
        $where[] = ['jie.comid','=',$this->userinfo['comid']];
        $where[] = ['jie.isturn','=',1];
        $where[] = ['jie.status','in',[0,1]];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['bill.sericnum|jieer.realname|xoss.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['sericnum']) && !empty($param['sericnum'])){
            $where[] = ['bill.sericnum','like', '%'.$param['sericnum'].'%'];
        }
        if(isset($param['jieername']) && !empty($param['jieername'])){
            $where[] = ['jieer.realname','like', '%'.$param['jieername'].'%'];
        }
        if(isset($param['xoss_name']) && !empty($param['xoss_name'])){
            $where[] = ['xoss.name','like', '%'.$param['xoss_name'].'%'];
        }


        $data['data'] = XossJieListModel::alias('list')
            ->field('jie.*,jieer.realname as jieername,dept.name as jieerdept,
            list.id as listid,list.explain as list_explain,list.guidt,list.guier,list.state,list.num,guier.realname as guiername,
            xoss.name as xoss_name,xoss.number as xoss_number,xoss.dangfen,e.name as enginname,bill.sericnum')
            ->join('xoss_jie jie','jie.id=list.jieid')
            ->leftJoin('flow_bill bill','bill.id=jie.billid')
            ->leftJoin('xoss xoss','xoss.id=list.xossid')
            ->leftJoin('engin e','e.id=xoss.enginid')
            ->leftJoin('user jieer','jieer.id=jie.jieer')
            ->leftJoin('dept dept','dept.id=jieer.deptid')
            ->leftJoin('user guier','guier.id=list.guier')
            ->where($where)
            ->where((new MenuService())->get_miss_where('xoss_jie', 'jie'))
            ->order('jie.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['dangfen_name'] = (new OptService())->get_base_showname('xoss_dangfen', $item['dangfen']);
                $item['gui_state_name'] = $item['state'] > 0 ? '已归还' : '未归还';
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('xoss_jie_detail');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_xoss_jie(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['jie.deleted_user','=', 0];
        $where[] = ['jie.comid','=',$this->userinfo['comid']];
        $where[] = ['jie.isturn','=',1];
        $where[] = ['jie.status','in',[0,1]];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['jieer.realname|jie.yong','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['jieername']) && !empty($param['jieername'])){
            $where[] = ['jieer.realname','like', '%'.$param['jieername'].'%'];
        }
        if(isset($param['yong']) && !empty($param['yong'])){
            $where[] = ['jie.yong','like', '%'.$param['yong'].'%'];
        }
        if(isset($param['yudt']) && !empty($param['yudt'])){
            $where[] = ['jie.yudt','between', explode(' - ', $param['yudt'])];
        }
        if(isset($param['state']) && $param['state'] != ''){
            $where[] = ['jie.state','=', $param['state']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['jie.status','=', $param['status']];
        }


        $data['data'] = XossJieModel::alias('jie')
            ->field('jie.*,jieer.realname as jieername,dept.name as deptname')
            ->leftJoin('user jieer','jieer.id=jie.jieer')
            ->leftJoin('dept dept','dept.id=jieer.deptid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('xoss_jie', 'jie'))
            ->order('jie.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $listOneXossid = XossJieListModel::where(['jieid'=>$item['id']])->value('xossid');
                $item['jietuname'] = XossModel::where(['id'=>$listOneXossid])->value('name');
                $listNum = XossJieListModel::where(['jieid'=>$item['id']])->count();
                if($listNum > 1){
                    $item['jietuname'] .= '...等'.$listNum.'本';
                }
                $item['state_name'] = (new XingService())->get_xoss_jie_state_name($item['state']);

                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('xoss_jie');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_xoss_jie(Request $request){
        $param = $request->post();
        $data = XossJieModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            XossJieModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'xoss_jie', $data['id'], '行政档案借阅');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_xoss_jie(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = XossJieModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $jieerinfo = UserModel::where(['id'=>$data['data']['jieer']])->find();
        $data['data']['jieer_name'] = $jieerinfo['realname'];
        $data['data']['dept_name'] = DeptModel::get_name_by_pk($jieerinfo['deptid']);

        $list = XossJieListModel::where(['jieid'=>$data['data']['id']])->select()->toArray();
        foreach ($list as &$item){
            $xossinfo = XossModel::where(['id'=>$item['xossid']])->find();
            if($xossinfo){
                $item['dangfen_name'] = (new OptService())->get_base_showname('xoss_dangfen', $xossinfo['dangfen']);

                $item['name'] = $xossinfo['name'];
                $item['number'] = $xossinfo['number'];
                $item['enginname'] = EnginModel::where(['id'=>$xossinfo['enginid']])->value('name');
            }
            $item['guier_name'] = UserModel::get_realname_by_pk($item['guier']);
        }
        $data['list'] = $list;

        $data['files'] = (new OptService())->get_file('xoss_jie', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }
    public function gui_xoss_jie(Request $request){
        if($request->isPost()){
            $param = $request->post();


            if(empty($param['guidt'])){
                return outputJson(4000, '请选择归还日期');
            }
            if(empty($param['guier'])){
                return outputJson(4000, '请选择接收人');
            }

            $param['state'] = 1;
            unset($param['token']);

            $listinfo = XossJieListModel::where(['id'=>$param['id']])->find();



            Db::startTrans();
            try {
                XossJieListModel::update($param);

                $jie_state = 1;
                $nohuan = XossJieListModel::where(['jieid'=>$listinfo['jieid'],'state'=>0])->find();
                if(!$nohuan){
                    $jie_state = 2;
                }
                XossJieModel::where(['id'=>$listinfo['jieid']])->update(['state'=>$jie_state]);


                (new OptService())->add_flow_log('行政档案归还', 'xoss_jie', $listinfo['id']);
                (new OptService())->add_log_list('行政档案归还', 'xoss_jie', $listinfo['id'], '行政档案借阅');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = XossJieListModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }
            $data['xossinfo'] = XossModel::where(['id'=>$data['data']['xossid']])->find();

            if($data['data']['state'] == 0){
                $data['data']['guidt'] = date("Y-m-d");
                $data['data']['guier_info'] = (new UserService())->get_users_part_arr($this->userinfo['id'], 'user');
            }else{
                $data['data']['guier_info'] = (new UserService())->get_users_part_arr($data['data']['guier'], 'user');
            }
            return outputJson(2000,lang('code_2000'),$data);
        }
    }


    public function list_doss_engin_hui(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['doss.deleted_user','=', 0];
        $where[] = ['doss.comid','=',$this->userinfo['comid']];
        $where[] = ['doss.dangfen','=',2];
        $where[] = ['doss.enginid','>',0];


        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['doss.enginid','=', $param['enginid']];
        }

        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['year']) && !empty($param['year'])){
            $where[] = ['shi.year','=', $param['year']];
        }
        if(isset($param['gongzi']) && !empty($param['gongzi'])){
            $where[] = ['e.gongzi','like', '%'.$param['gongzi'].'%'];
        }
        if(isset($param['gongnei']) && !empty($param['gongnei'])){
            $where[] = ['e.gongnei','like', '%'.$param['gongnei'].'%'];
        }
        if(isset($param['guimo']) && !empty($param['guimo'])){
            $where[] = ['e.guimo','like', '%'.$param['guimo'].'%'];
        }
        if(isset($param['kailei']) && !empty($param['kailei'])){
            $where[] = ['e.kailei','=', $param['kailei']];
        }
        if(isset($param['start_money']) && !empty($param['start_money'])){
            $where[] = ['shi.money','>=',$param['start_money']];
        }
        if(isset($param['end_money']) && !empty($param['end_money'])){
            $where[] = ['shi.money','<=',$param['end_money']];
        }
        if(isset($param['startdt']) && !empty($param['startdt'])){
            $where[] = ['shi.startdt','between', explode(' - ', $param['startdt'])];
        }
        if(isset($param['enddt']) && !empty($param['enddt'])){
            $where[] = ['shi.enddt','between', explode(' - ', $param['enddt'])];
        }

        $data['year'] = (new ContractService())->get_contract_yeararr();
        $data['is_year'] = isset($param['year']) ? $param['year'] : '';

        $data['kailei'] = (new OptService())->get_base_info('bid_kailei');
        $data['is_kailei'] = isset($param['kailei']) ? $param['kailei'] : '';


        $data['data'] = DossModel::alias('doss')
            ->field('doss.enginid,e.name,e.gongnei,e.guimo,e.gongzi,e.kailei
            ,shi.year,shi.money,shi.startdt,shi.enddt')
            ->leftJoin('engin e','e.id=doss.enginid')
            ->leftJoin('contract_shi shi','e.id=shi.enginid and shi.deleted_user=0 and shi.isturn=1 and shi.status=1')
            ->where($where)
            ->group('doss.enginid')
            ->order('doss.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['kailei_name'] = (new OptService())->get_base_showname('bid_kailei', $item['kailei']);
                $jinger = CertUseListModel::alias('list')
                    ->field('use.*,e.state,e.name')
                    ->join('cert_use use','use.id = list.useid')
                    ->leftJoin('engin e','e.id = use.enginid')
                    ->leftJoin('cert cert','cert.id = list.certid')
                    ->leftJoin('cert_person person','person.id = cert.personid')
                    ->where(['use.comid'=>$this->userinfo['comid'],'use.deleted_user'=>0])
                    ->where(['use.isturn'=>1,'use.status'=>1,'use.isreal'=>1,'enginid'=>$item['enginid']])
                    ->where(['enginid'=>$item['enginid'],'list.place'=>'项目经理'])
                    ->column('person.name');
                $item['jinger'] = implode(',', $jinger);

                $jier = CertUseListModel::alias('list')
                    ->field('use.*,e.state,e.name')
                    ->join('cert_use use','use.id = list.useid')
                    ->leftJoin('engin e','e.id = use.enginid')
                    ->leftJoin('cert cert','cert.id = list.certid')
                    ->leftJoin('cert_person person','person.id = cert.personid')
                    ->where(['use.comid'=>$this->userinfo['comid'],'use.deleted_user'=>0])
                    ->where(['use.isturn'=>1,'use.status'=>1,'use.isreal'=>1,'enginid'=>$item['enginid']])
                    ->where(['enginid'=>$item['enginid'],'list.place'=>'技术负责人'])
                    ->column('person.name');
                $item['jier'] = implode(',', $jier);

                $item['zhuang'] = '在建';
                $jun = DossModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['enginid'],'dangfen'=>7])->find();
                if($jun){
                    $item['zhuang'] = '完工';
                }

                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_doss_engin_hui222(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['doss.deleted_user','=', 0];
        $where[] = ['doss.comid','=',$this->userinfo['comid']];
        $where[] = ['doss.dangfen','=',2];
        $where[] = ['doss.enginid','>',0];


        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['doss.enginid','=', $param['enginid']];
        }

        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['year']) && !empty($param['year'])){
            $where[] = ['shi.year','=', $param['year']];
        }
        if(isset($param['gongzi']) && !empty($param['gongzi'])){
            $where[] = ['e.gongzi','like', '%'.$param['gongzi'].'%'];
        }
        if(isset($param['gongnei']) && !empty($param['gongnei'])){
            $where[] = ['e.gongnei','like', '%'.$param['gongnei'].'%'];
        }
        if(isset($param['guimo']) && !empty($param['guimo'])){
            $where[] = ['e.guimo','like', '%'.$param['guimo'].'%'];
        }
        if(isset($param['start_money']) && !empty($param['start_money'])){
            $where[] = ['shi.money','>=',$param['start_money']];
        }
        if(isset($param['end_money']) && !empty($param['end_money'])){
            $where[] = ['shi.money','<=',$param['end_money']];
        }
        if(isset($param['startdt']) && !empty($param['startdt'])){
            $where[] = ['shi.startdt','between', explode(' - ', $param['startdt'])];
        }
        if(isset($param['enddt']) && !empty($param['enddt'])){
            $where[] = ['shi.enddt','between', explode(' - ', $param['enddt'])];
        }

        $data['year'] = (new ContractService())->get_contract_yeararr();
        $data['is_year'] = isset($param['year']) ? $param['year'] : '';

        $list = [];
        $info = DossModel::alias('doss')
            ->field('doss.enginid,e.name,e.gongnei,e.guimo,e.gongzi
            ,shi.year,shi.money,shi.startdt,shi.enddt')
            ->leftJoin('engin e','e.id=doss.enginid')
            ->leftJoin('contract_shi shi','e.id=shi.enginid and shi.deleted_user=0 and shi.isturn=1 and shi.status=1')
            ->where($where)
            ->group('doss.enginid')
            ->order('doss.id DESC')
            ->select()->toArray();
        foreach ($info as $item){
            $jinger = CertUseListModel::alias('list')
                ->field('use.*,e.state,e.name')
                ->join('cert_use use','use.id = list.useid')
                ->leftJoin('engin e','e.id = use.enginid')
                ->leftJoin('cert cert','cert.id = list.certid')
                ->leftJoin('cert_person person','person.id = cert.personid')
                ->where(['use.comid'=>$this->userinfo['comid'],'use.deleted_user'=>0])
                ->where(['use.isturn'=>1,'use.status'=>1,'use.isreal'=>1,'enginid'=>$item['enginid']])
                ->where(['enginid'=>$item['enginid'],'list.place'=>'项目经理'])
                ->column('person.name');
            $item['jinger'] = implode(',', $jinger);

            $jier = CertUseListModel::alias('list')
                ->field('use.*,e.state,e.name')
                ->join('cert_use use','use.id = list.useid')
                ->leftJoin('engin e','e.id = use.enginid')
                ->leftJoin('cert cert','cert.id = list.certid')
                ->leftJoin('cert_person person','person.id = cert.personid')
                ->where(['use.comid'=>$this->userinfo['comid'],'use.deleted_user'=>0])
                ->where(['use.isturn'=>1,'use.status'=>1,'use.isreal'=>1,'enginid'=>$item['enginid']])
                ->where(['enginid'=>$item['enginid'],'list.place'=>'技术负责人'])
                ->column('person.name');
            $item['jier'] = implode(',', $jier);

            $item['zhuang'] = '在建';
            $jun = DossModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                ->where(['enginid'=>$item['enginid'],'dangfen'=>7])->find();
            if($jun){
                $item['zhuang'] = '完工';
            }


            $res = true;
            if(isset($param['jinger']) && !empty($param['jinger'])){
                if(strpos($item['jinger'], $param['jinger']) === false){
                    $res = false;
                }
            }
            if(isset($param['jier']) && !empty($param['jier'])){
                if(strpos($item['jier'], $param['jier']) === false){
                    $res = false;
                }
            }
            if(isset($param['zhuang']) && !empty($param['zhuang'])){
                if($item['zhuang'] != $param['zhuang']){
                    $res = false;
                }
            }

            if($res){
                $list[] = $item;
            }
        }




        $data['data'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_doss_engin_files(Request $request){
        $param = $request->post();

        //点击项目名称，弹出 附件信息，样式参照详情页的附件信息，弹小框
        //内容为：中标通知书，建设工程施工合同，开工报告，竣工报告，竣工验收报告，结算审核报告
        //分别取工程档案库中，分类为以上的数据的附件。
        //中标通知书【1】，建设工程施工合同【2】，开工报告【5】，竣工报告【6】，竣工验收报告【7】，结算审核报告【9】

        $where[] = ['deleted_user','=', 0];
        $where[] = ['comid','=',$this->userinfo['comid']];
        $where[] = ['enginid','=',$param['enginid']];


        $list = [1,2,5,6,7,9];
        $list = [
            ['id'=>1, 'name'=>'中标通知书' ,'table'=>'ff1'],
            ['id'=>2, 'name'=>'建设工程施工合同' ,'table'=>'ff2'],
            ['id'=>5, 'name'=>'开工报告' ,'table'=>'ff5'],
            ['id'=>6, 'name'=>'竣工报告' ,'table'=>'ff6'],
            ['id'=>7, 'name'=>'竣工验收报告' ,'table'=>'ff7'],
            ['id'=>9, 'name'=>'结算审核报告' ,'table'=>'ff9'],
        ];
        foreach ($list as &$vvv){
            $dossIdArr = DossModel::where($where)->where(['dangfen'=>$vvv['id']])->column('id');
            $files = FileModel::where(['comid'=>$this->userinfo['comid'],'table'=>'doss'])->where('mid','in',$dossIdArr)->select()->toArray();
            $vvv['children'] = $files;
        }


        $data['files'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }


}
